1 / 46

JC Raymond, Education, SAS Bill Fehlner, Education, SAS

Programming Techniques in SAS: Using the Macro Facility. Programming Techniques in SAS: Using the Macro Facility. &. %. &. &. %. &. %. %. %. &. %. &. &. %. %. %. &. &. %. &. &. %. &. %. %. &. %. %. %. &. %. &. &. &. %. &. &. %. &. &. &. &. %. %. &.

maurizio
Download Presentation

JC Raymond, Education, SAS Bill Fehlner, Education, SAS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 1. Reuse Dynamic Code demo1_dynamicAutoexec.sas

  5. 1. Reuse Dynamic Code The %Greeting Macro

  6. 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.

  7. 1. Reuse Dynamic Code Macro Variable Dictionary

  8. 1. Reuse Dynamic Code Macro Variable Dictionary

  9. 1. Reuse Dynamic Code Delete Variables

  10. 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

  11. 2. Automated Process

  12. 2. Automated Process Using The %Benchmark Macro demo2_runBenchmark.sas

  13. 2. Automated Process The %Benchmark Macro

  14. 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.

  15. 2. Automated Process The %Benchmark Macro

  16. 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.

  17. 2. Automated Process The %Benchmark Macro

  18. 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.

  19. 3. Automated Job Control demo3_demostrateJobControl.sas

  20. 3. Automated Job Control The %Jobcontrol Macro

  21. 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.

  22. 4. Extend SQL demo4_surrogateKeys.sas

  23. 4. Extend SQL The %Nextval Macro

  24. 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.

  25. 4. Extend SQL The %Nextval Macro

  26. 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.

  27. 5. Pure Macros without data steps demo5_QueryDescriptor.sas

  28. 5. Pure Macros The %obsnvars Macro

  29. 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.

  30. 6. Recursive Programming http://www.mantasoft.co.uk/_stuff/Recursive.htm n! n! = gamma(n+1) ; demo6_Factorial.sas

  31. 7. Divide & Conquer Not Sorted Not Sorted SORT SORT Sorted Sorted demo7_DivideAndConquer.sas

  32. 7. Divide & Conquer The actual input data

  33. 7. Divide & Conquer demo7_DivideAndConquer.log

  34. 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.

  35. 7. Divide & Conquer PROC SORT TIME -> 0:22:20.658 SORT MACRO TIME -> 0:09:21.227 A SAVING OF 0:12:59 (58%)

  36. 7. Divide & Conquer The %SORT macro

  37. 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.

  38. 7. Divide & Conquer The %SORT macro 1 Dataset having a length of less than MAXREC Split Dataset into Several Sub-datasets While Sorting

  39. 7. Divide & Conquer The %SORT macro

  40. 7. Divide & Conquer The %SORT macro

  41. 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

  42. 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

  43. How To Learn More • Technical Support • http://support.sas.com/techsup/intro.html • http://support.sas.com/techsup/faq/macro.html

  44. 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

  45. Questions ?

More Related