dictionary tables in proc sqlsome utility applications of …msz03/epi697/papers/sql_dict.pdf ·...

Download Dictionary Tables in PROC SQLSome Utility Applications Of …msz03/epi697/papers/sql_dict.pdf · sqlutilov.doc 3:38 PM 28 Apr 1999 Page 1 of 33 Some Utility Applications Of The Dictionary

If you can't read please download the document

Upload: dinhquynh

Post on 08-Feb-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 1 of 33

    Some Utility Applications Of TheDictionary Tables in PROC SQL

    Jack Hamilton

    First HealthWest Sacramento, California

    [email protected]

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 2 of 33

    What Are The Dictionary Tables? The Dictionary Tables in SQL are maintained by

    the SAS System. They contain information about various user and

    system objects. They're called tables in the documentation, but in

    some sense they're really views - theyre notstored anywhere.

    Dictionary Tables are available only in PROC SQL,but views of all the tables are available through theSASHELP library.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 3 of 33

    When To Use the Dictionary Tables Sometimes you need information about the

    structure of your data. Dictionary tables are one source of this

    information. Other sources are PROC CONTENTS, PROC

    CATALOG, and PROC FORMAT, and the data stepinformation functions.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 4 of 33

    How To Specify The Dictionary TablesA dictionary table is used just like any other table inPROC SQL:

    select *from dictionary.tables;

    SASHELP views are used like any other views:

    proc print data=sashelp.vtable;run;

    Either piece of code would print a lot of informationabout all available tables.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 5 of 33

    The SQL Tables and SASHELP Views The following pages list the names of the SQL

    dictionary tables, along with their SASHELP viewequivalents.

    A complete list of tables can be found in the onlinehelp for PROC SQL.

    A complete list of SASHELP views can be createdwith the following code:

    proc print data=sashelp.vsview;where libname = 'SASHELP' and

    substr(memname, 1, 1) = 'V';run;

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 6 of 33

    Brief Description of the Tables CATALOGS describes every member of every

    catalog in every library. COLUMNS describes every column in every table

    or view. EXTFILES describes every defined fileref,

    showing the path and engine. INDEXES describes every index on every dataset

    in every library.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 7 of 33

    Brief Description of the Tables MACROS describes every active macro variable. MEMBERS describes objects shared by tables,

    views, and catalogs TITLES contains the current titles and footnotes. OPTIONS contains most of the current portable

    system options. TABLES describes every table and view in very

    library. VIEWS describes every available view.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 8 of 33

    dictionary.catalogs(sashelp.vcatalg)

    LIBNAME char(8) Library NameMEMNAME char(8) Member NameMEMTYPE char(8) Member TypeOBJNAME char(8) Object NameOBJTYPE char(8) Object TypeOBJDESC char(40) Object DescriptionMODIFIED char(8) Date ModifiedALIAS char(8) Object Alias

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 9 of 33

    dictionary.columns(sashelp.vcolumn)

    LIBNAME char(8) Library NameMEMNAME char(8) Member NameMEMTYPE char(8) Member TypeNAME char(8) Column NameTYPE char(4) Column TypeLENGTH num Column LengthNPOS num Column PositionVARNUM num Column Number in TableLABEL char(40) Column LabelFORMAT char(16) Column FormatINFORMAT char(16) Column InformatIDXUSAGE char(9) Column Index Type

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 10 of 33

    dictionary.extfiles(sashelp.vextfl)

    FILEREF char(8) FilerefXPATH char(80) Path NameXENGINE char(8) Engine Name

    Its possible to have a path name thats longerthan 80 characters, so be careful with this one.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 11 of 33

    dictionary.indexes(sashelp.vindex)

    LIBNAME char(8) Library NameMEMNAME char(8) Member NameMEMTYPE char(8) Member TypeNAME char(8) Column NameIDXUSAGE char(9) Column Index TypeINDXNAME char(8) Index NameINDXPOS num Position of Column

    in Concatenated KeyNOMISS char(3) Nomiss OptionUNIQUE char(3) Unique Option

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 12 of 33

    dictionary.members(sashelp.vmember)

    LIBNAME char(8) Library NameMEMNAME char(8) Member NameMEMTYPE char(8) Member TypeENGINE char(8) Engine NameINDEX char(8) IndexesPATH char(80) Path Name

    This table duplicates some of the informationfound in other tables. The important addition isthe PATH variable, but see the note on EXTFILES.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 13 of 33

    dictionary.macros(sashelp.vmacro)

    SCOPE char(9) Macro ScopeNAME char(8) Macro Var NameOFFSET num Offset into Macro VarVALUE char(200) Macro Var Value

    If a macro variables value is longer than 200characters, there will be multiple entries in thistable. The first will have an OFFSET of 0, thesecond of 200, and so forth.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 14 of 33

    dictionary.titles(sashelp.vtitle)

    TYPE char(1) Title LocationNUMBER num Title NumberTEXT char(200) Title Text

    Only the text is stored in these variables, not thefont and positioning information used bySAS/GRAPH.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 15 of 33

    dictionary.options(sashelp.voption)

    OPTNAME char(16) Session Option NameSETTING char(200) Session Option SettingOPTDESC char(80) Option Description

    It's worth noting that this table probably containsonly portable options, not platform-dependentoptions. That does seem to vary slightly byrelease.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 16 of 33

    table dictionary.tables(sashelp.vtable)

    LIBNAME char(8) Library NameMEMNAME char(8) Member NameMEMTYPE char(8) Member TypeMEMLABEL char(40) Dataset LabelTYPEMEM char(8) Dataset TypeCRDATE num Date CreatedMODATE num Date ModifiedNOBS num Number of ObservationsOBSLEN num Observation LengthNVAR num Number of VariablesPROTECT char(3) Type of Password Prot.COMPRESS char(8) Compression RoutineREUSE char(3) Reuse SpaceBUFSIZE num BufsizeDELOBS num Number of Deleted Obs

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 17 of 33

    INDXTYPE char(9) Type of Indexes

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 18 of 33

    dictionary.views(sashelp.vview)

    LIBNAME char(8) Library NameMEMNAME char(8) Member NameMEMTYPE char(8) Member TypeENGINE char(8) Engine Name

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 19 of 33

    How Would You Use These Tables?

    Decide what you need to do. Figure out how you might do whatever you want to

    do if you were coding it manually. Figure out which table contains the information

    you need. Note some information is contained inseveral tables - if you wanted the names of alldatasets, for example, you could use MEMBERS,TABLES, or COLUMNS.

    Use SQL or a data step to create the code. Run it.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 20 of 33

    Some Useful Techniques

    Use the INTO and SEPARATED BY clauses inPROC SQL to create a macro variable.

    Use CALL EXECUTE in a data step. Use a FILENAME pointed to a catalog entry, and

    %INCLUDE the results (this is preferable to writingto a temporary disk file, which is not platform-independent).

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 21 of 33

    A Sample Dataset

    1 data one;2 key='9'; a=1; c=2; b=.;3 output;4 key='0'; c=12; b=1;a=0;5 output;6 put all;7 /* Shows order of vars */8 run;

    KEY=0 A=0 C=12 B=1 _ERROR_=0_N_=1NOTE: The data set WORK.ONE has 2observations and 4 variables.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 22 of 33

    Ex. 1: Put Vars Into Alpha OrderThere are several techniques for reordering thevariables in a SAS dataset. This first example usesPROC SQL. You might code it manually like this:

    proc sql;create table two as

    select A,B,C,KEYfrom one;

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 23 of 33

    Ex. 1: Put Vars Into Alpha OrderThe fields you need to create this code are the table name (library and member) the column namesThis information can be obtained from theDICTIONARY.COLUMNS table.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 24 of 33

    Ex. 1: Put Vars Into Alpha Order

    proc sql noprint;select nameinto :newcmdseparated by ','from dictionary.columnswhere libname='WORK' and

    memname='ONE'order by name;

    create table two asselect &NEWCMD.from one;

    NEWCMD has the value A, B, C, KEY

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 25 of 33

    Ex. 2: Convert Char to NumericConverting a large number of character variables tonumeric seems to be a common task. SQL providesan easy solution:

    create table three asselect input(key, best.),

    a, b, cfrom one;

    Again, the information you need can be obtainedfrom DICTIONARY.COLUMNS.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 26 of 33

    Ex. 2: Convert Char to Numericselect

    case typewhen 'num' then nameelse 'input(' || name || ',

    best.) as ' || nameend

    into :newcmd separated by ','from dictionary.columnswhere libname='WORK'

    and memname='ONE';create table three as

    select &NEWCMD.from one;

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 27 of 33

    Ex. 3: Rename VariablesSuppose you have a number of variables withinconvenient names, and you want to rename themall:

    data sample;retain xx0y01-xx0y10 0;output; stop;

    run;In this case, suppose you want to rename variableXX0Y01 to XXY01 and so forth.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 28 of 33

    Ex. 3: Renaming VariablesOne reasonable way to do the renames is withPROC DATASETS; just changing the namesdoesn't require rewriting the data.

    proc datasets library=worknolist;

    modify sample;rename XX0Y01=XXY01

    XX0Y01=XXY01; /* etc. */run; quit;

    Once more, you need the information inDICTIONARY.COLUMNS.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 29 of 33

    Ex. 3: Renaming VariablesSo here's the code. Once you understand what'sit's doing, it's straightforward.

    proc sql noprint;select name || '=' ||

    substr(name, 1, 2) ||substr(name, 4, 3)

    into :renames separated by ' 'from dictionary.columnswhere libname='WORK' and

    memname='SAMPLE' andname like 'XX0Y__';

    quit;

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 30 of 33

    Ex. 3: Renaming VariablesAt this point, RENAMES will have the value

    XX0Y01=XXY01 XX0Y02=XXY02 XX0Y03=XXY03 XX0Y04=XXY04XX0Y05=XXY05 XX0Y06=XXY06 XX0Y07=XXY07 XX0Y08=XXY08XX0Y09=XXY09 XX0Y10=XXY10

    You can then use it in PROC DATASETS:

    proc datasets library=worknolist;

    modify sample;rename &RENAMES.;

    run; quit;

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 31 of 33

    Ex. 4: List FormatsFor documentation, it might be helpful to have a listof all formats in a library. You can get that withSASHELP.VCATALG:

    proc print data=sashelp.vcatalg;where objtype in('FORMAT', 'FORMATC', 'INFMT',

    'INFMTC');run;

    This information must be combined with the list ofcatalogs specified in the FMTSEARCH option tofind all available formats.

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 32 of 33

    Ex. 5: Place Varnames Into MacvarIts occasionally useful to have a macro variablecontaining the names of all variables in a dataset,except specified ones (see the FIRST./LAST.example in my paper The Problem WithNODUPLICATES, Continued):

    proc sql noprint;select nameinto :vnames separated by ' 'from dictionary.columnswhere libname = 'SASUSER'

    and memname = 'IRIS'and name not in

    ('SEPALLEN','PETALLEN');

  • sqlutilov.doc 3:38 PM 28 Apr 1999 Page 33 of 33

    Cautions The examples in this paper don't do any error

    checking. You should. Make sure your code works correctly if nothing is

    selected by the WHERE clause. You probablyneed to set the macro variable to blank before youstart.

    Not all tables and views are available in allreleases of SAS.

    The COLUMNS table can get very large,especially if you have SAS/GRAPH maps. Apply aWHERE clause as soon as you can.