460 likes | 592 Views
Programming Techniques in SAS: Using the Macro Facility. Programming Techniques in SAS: Using the Macro Facility. &. %. &. &. %. &. %. %. %. &. %. &. &. %. %. %. &. &. %. &. &. %. &. %. %. &. %. %. %. &. %. &. &. &. %. &. &. %. &. &. &. &. %. %. &.
E N D
Programming Techniques in SAS: Using the Macro Facility Programming Techniques in SAS: Using the Macro Facility & % & & % & % % % & % & & % % % & & % & & % & % % & % % % & % & & & % & & % & & & & % % & & JC Raymond, Education, SAS Bill Fehlner, Education, SAS 514 395-4087 jc.raymond@sas.com 416 307-4513 bill.fehlner@sas.com
Programming Examples Reuse dynamic code Automated Process Automate job control Extend SQL with custom functions Pure macros without data steps The Factorial Divide and Conquer
Some Macro Building Blocks • Macro programs with parameters • Macro Variables stored in Symbol tables • Automatic variables, e.g. System return codes • Run-time functions, e.g. the resolve function. • Macro Functions • Recursive programming
1. Reuse Dynamic Code demo1_dynamicAutoexec.sas
1. Reuse Dynamic Code The %Greeting Macro
1. Reuse Dynamic Code The %Greeting Macro • &sysuserid contains the userid of the SAS session • Keyword parameters have a default value. • A %macro statement begins a macro definition and a %mend statement ends it. • %put statements write to the SAS log.
1. Reuse Dynamic Code Macro Variable Dictionary
1. Reuse Dynamic Code Macro Variable Dictionary
1. Reuse Dynamic Code Delete Variables
2. Automated Process Final report Process parameters Sas data sets Proc tabulate Create sample data Sas data set Task 2 Task 2 Task 1 obs=1 Task 1 Task 1 Data step reads log Sas log Route log file to disk Task 2 obs=1
2. Automated Process Using The %Benchmark Macro demo2_runBenchmark.sas
2. Automated Process The %Benchmark Macro
2. Automated Process Take-aways • %local insures no interference with variables potentially existing outside of the macro procedure. • A macro program can check for a null parameter.
2. Automated Process The %Benchmark Macro
2. Automated Process Take-aways • %include allows blocks of code to be stored in text files and inserted into the program code at any time. • %scan can be used to split a parameter value into multiple units. • Title statements (and any others) can be dynamic in the macro world.
2. Automated Process The %Benchmark Macro
2. Automated Process Take-aways • Proc Printto can route the SAS log to a text file. • Multiple ampersand expressions select one macro variable from a named group of macro variables.
3. Automated Job Control demo3_demostrateJobControl.sas
3. Automated Job Control The %Jobcontrol Macro
3. Automated Job Control Take-aways • The parmbuff option permits a variable number of parameters in a macro call. • %scan can be used to select a parameter from the parameter string stored in the &syspbuff macro. • &syspbuff contains the entire parameter string. The %str(%(,%)) specifies that a right parenthesis, a comma, and a left parenthesis are delimiters for the scan. • %eval( ) function permits integer arithmetic in a macro expression.
4. Extend SQL demo4_surrogateKeys.sas
4. Extend SQL The %Nextval Macro
4. Extend SQL Take-aways • Macro parameters can be passed “by reference”. In this case constantName contains a reference to a macro variable • &constantName inserts the name of the target macro variable. • &&&constantName inserts the value of the target macro variable. • &&&constantName appearing outside of a macro statement supplies the return value for the nextval macro program.
4. Extend SQL The %Nextval Macro
4. Extend SQL Take-aways • The input( ) function converts its character argument to a numeric value. • The resolve( ) function processes a macro expression (in this case the %nextval macro program) and returns any text produced. • The argument to the resolve( ) function is in single quotes so it does not get processed until run time.
5. Pure Macros without data steps demo5_QueryDescriptor.sas
5. Pure Macros The %obsnvars Macro
5. Pure Macros Take-aways • %global and %local allow you to determine where macro variables are stored. • %sysfunc( ) allows the macro processor to use data step functions directly. • Open( ), attrn( ) and close( ) functions access the data set and read attribute values from the descriptor. • %sysfunc(sysmsg( ) ) captures error message text.
6. Recursive Programming http://www.mantasoft.co.uk/_stuff/Recursive.htm n! n! = gamma(n+1) ; demo6_Factorial.sas
7. Divide & Conquer Not Sorted Not Sorted SORT SORT Sorted Sorted demo7_DivideAndConquer.sas
7. Divide & Conquer The actual input data
7. Divide & Conquer demo7_DivideAndConquer.log
7. Divide & Conquer Take Away • %sysfunc(time(),best15.) allows the macro processor to use data step functions directly and format the result. • %sysevalf(&to - &from) performs floating-point arithmetic and returns a value that is formatted using the BEST32. format. • %sysfunc(putn(%sysevalf(&to - &from),time12.3)).%sysfunc cannot format the result of the %sysevalf since it is expecting a function as argument; reason why we must used putn.
7. Divide & Conquer PROC SORT TIME -> 0:22:20.658 SORT MACRO TIME -> 0:09:21.227 A SAVING OF 0:12:59 (58%)
7. Divide & Conquer The %SORT macro
7. Divide & Conquer Take Away • %macro sort(dsnin,dsnout,by,maxrec) ; allows passing parameters by position call symput('dsn' || trim(left(put(i,3.))), '___' || trim(left(put(i,3.)))) ; • The macro variables dsn1, dsn2, dsn3 … dsnnwill contain “___1, ___2, ___3 … ___n”; the name of the temporary sorted datasets. • NB_WRK is the number of temporary datasets. • MAXREC is the maximum number of observation per Sort. • NOBS is the total number of observations to be sorted from the input dataset.
7. Divide & Conquer The %SORT macro 1 Dataset having a length of less than MAXREC Split Dataset into Several Sub-datasets While Sorting
7. Divide & Conquer The %SORT macro
7. Divide & Conquer The %SORT macro
How To Learn More • Instructor based training: • http://support.sas.com/training/Canada • Next “SAS Macro Language”, a two-day course, starting on • February 25th in Montreal • February 2nd in Ottawa • December 15th in Toronto • Next “SAS Macro Language: Advanced Topics”, a one-day course on • March 26th in Montreal • February 11th in Ottawa and • February 16th in Toronto
How To Learn More • Instructor based training: • http://support.sas.com/training/Canada • Next “SAS Macro Language”, a two-day course, starting on • February 25th in Montreal • February 2nd in Ottawa • December 15th in Toronto • Next “SAS Macro Language: Advanced Topics”, a one-day course on • March 26th in Montreal • February 11th in Ottawa and • February 16th in Toronto
How To Learn More • Technical Support • http://support.sas.com/techsup/intro.html • http://support.sas.com/techsup/faq/macro.html
How To Learn More • Books • SAS Guide to Macro Processing #56041 • SAS Macro Language Reference #55501 • SAS Macro Facility Tips & Techniques #55097 • Carpenter’s Complete Guide to the SAS Macro Language #56100