Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables,...
Transcript of Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables,...
![Page 1: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/1.jpg)
Macro variables, proc sql and the data dictionary
The efficiency quest
![Page 2: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/2.jpg)
Objectives
Flexibility
Efficiency
Laziness
![Page 3: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/3.jpg)
Examples of macro variable lists
“charval1”,”charval2”,”charval3”,….
4506005350205432, 3700120043210987, 2208543456709876
var1 var2 var3 var4 …..
![Page 4: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/4.jpg)
¯ovarlist in proc and data step statements
1. where=(numvar in (¯ovarlist)) ;
2. where=(charvar in (¯ovarlist)) ;
3. proc whatever data=ds1 (where=(charvar in (¯ovarlist)));
4. proc whatever ; var ¯ovarlist ;
5. set abc (keep = ¯ovarlist) ;
6. where var in (¯ovarlist) ;
7. set ¯ovarlist ;
![Page 5: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/5.jpg)
Proc SQL features
select metadata from dictionary ;
select values into :macrovarlist separated by „delimiter‟ ;
The dictionary is created for each SAS session and updated dynamically.
Sashelp.vtables is a view on dictionary.tables
![Page 6: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/6.jpg)
Scenario #1
Client sends e-mail with list of accounts to be matched then sorted. Steps
1. Paste the list into program editor and wrap with data step and datalines statement.
2. Create a macro variable list using proc sql.
3. Subset a large master data source to quickly extract the client‟s data.
![Page 7: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/7.jpg)
A typical client request
Dear Analyst,
I need everything about these accounts:
4500396774285192
4500962650797314
4500296321583120
4500469120742973
As soon as possible.
![Page 8: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/8.jpg)
Input values into dataset
data accts ;
input acct_num $16. ;
datalines ;
4501180048137018
4500600001486376
4500600002909772
4500607700775017
;
run ;
![Page 9: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/9.jpg)
Create a quoted list
proc sql noprint ;
select quote(acct_char) into :acctlist
separated by ','
from cardlist ;
quit ;
%put &acctlist ;
"4500396774285192","4500962650797314","4500296321583120","4500469120742973"
![Page 10: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/10.jpg)
Subset using list
data subset ;
set demodata.billrecs (where=(acct_char in (&acctlist))) ;
run ;
BUT
Most Databases do not like double quotes
![Page 11: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/11.jpg)
Create a single quoted list
proc sql noprint ;
select "'"||left(trim(acct_char))||"'" into :acctlist1
separated by ','
from cardlist ;
quit ;
%put &acctlist1 ;
‘4500396774285192’,’4500962650797314’,’4500296321583120’,’4500469120742973’
![Page 12: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/12.jpg)
Efficient DB subsetting
proc sql;
connect to db2 (ssid= dsn);
create table out2 as select * from connection to db2
(select * from db2table
where acct_char in (&acctlist1) );
disconnect from db2;
quit;
![Page 13: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/13.jpg)
Scenario #2
Need to access data across range of libraries or datasets.
Create ¯ovarlist from data dictionary based on a pattern.
Use ¯ovarlist after a set statement in a data step.
![Page 14: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/14.jpg)
Similar tables in a library
proc sql noprint ;
select 'SASHELP.'||left(memname)
into :memlist separated by ' '
from dictionary.members
where libname = 'SASHELP'
and memname like 'NVST%' ;
quit ;
%put &memlist ;
SASHELP.NVST1 SASHELP.NVST2 etc.
![Page 15: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/15.jpg)
Create concatenation
data all_nvst ;
set &memlist ;
run ;
proc print data = all_nvst ;
run ;
![Page 16: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/16.jpg)
Concatenation views
data all_nvst_v /view= all_nvst_v ;
set &memlist ;
run ;
Minimal creation time
Minimal storage
Dynamic
![Page 17: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/17.jpg)
Dictionary data (v8) Proc sql SASHELP
dictionary.tables dictionary.columns dictionary.indexes dictionary.views dictionary.macros dictionary.extfiles dictionary.options dictionary.catalogs dictionary.titles
Sashelp.vtable Sashelp.vcolumn Sashelp.vindex Sashelp.vview Sashelp.vmacro Sashelp.vextfl Sashelp.vcolumn Sashelp.vcatalg Sashelp.vtitle
![Page 18: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/18.jpg)
Additional dictionary data (v9) Proc sql SASHELP
dictionary.check_constraints
dictionary.constraint_column_usage
dictionary.constraint_table_usage
dictionary.dataitems
dictionary.destinations
dictionary.dictionaries
dictionary.engines
dictionary.filters
dictionary.functions
dictionary.goptions
dictionary.infomaps
dictionary.prompts
dictionary.promptsxml
dictionary.referential_constraints
dictionary.remember
dictionary.styles
dictionary.table_constraints
sashelp.vallopt
sashelp.vbplayrs
sashelp.vdatait
sashelp.vdctnry
sashelp.vdest
sashelp.vengine
sashelp.verbmgr
sashelp.vfilter
sashelp.vformat
sashelp.vfunc
sashelp.vgopt
sashelp.vidmsg
sashelp.vinfomp
sashelp.vlibnam
sashelp.vmember
sashelp.vprmxml
sashelp.vprompt
sashelp.vrefcon
sashelp.vrememb
sashelp.vtabcon
![Page 19: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/19.jpg)
proc sql noprint;
select distinct cats
("describe table dictionary.",memname,";")
into :describeAll separated by ' '
from dictionary.dictionaries
;
&describeAll
quit;
![Page 20: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/20.jpg)
Additional information
For some useful DB2 features see Harry Droogendyk‟s data driven coding paper:
http://www.stratia.ca/papers/QC_Using_Dictionary_Tables.pdf
Peter Eberhardt‟s extensive review of the SAS data dictionary
www2.sas.com/proceedings/sugi31/259-31.pdf
![Page 21: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/21.jpg)
Explicit data step looping
Adds control over the SAS data step
Useful comparison with other languages
More logical sequence of SAS statements
![Page 22: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/22.jpg)
Scenario #4
Need to create header and trailer on a flat file from SAS dataset.
Write header first, then formatted data, then trailer with totals.
Write trailer when do loop completes.
![Page 23: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/23.jpg)
Controlling the loop
data _null_ ; put 'Header text' ; do until (last) ; set demodata.billrecs end = last ; put variables ; end ; put 'Trailer text' ; stop ; run ;
![Page 24: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/24.jpg)
data _null_ ; file "!TMP\Example8_acctlist.txt" ; /* Target */ rundate = today() ; put @1 'List created on ' rundate yymmdd10. ; /* Header */ do until (last) ; /* Loop control */ set demodata.billrecs /* Source inside loop */ (where=(acct_char in (&acctlist))) /* Filter */ end = last ; /* Terminates loop */ n ++ 1 ; /* Counter */ put @1 acct_char $16. ; /* Output record */ end ; put @1 'Record count = ' @14 n z10. ; /* Trailer */ stop ; /* Good precaution */ run ;
![Page 25: Macro variables, proc sql and the data dictionary - SAS Group Presentations... · Macro variables, proc sql and the data dictionary The efficiency quest . ... Proc sql SASHELP dictionary.check_constraints](https://reader031.fdocuments.us/reader031/viewer/2022020109/5c665a2809d3f2d12a8c2cd2/html5/thumbnails/25.jpg)
Benefits
„Toolbox‟ utilities with many applications
Data driven programming
Flexibility
Accuracy
Automation