Title: STS Customer Insight in Marketing January 2003
1Programming 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_at_sas.com 416
307-4513 bill.fehlner_at_sas.com
2(No Transcript)
3Programming Examples
- Reuse dynamic code
- Automated Process
- Automate job control
- Extend SQL with custom functions
- Pure macros without data steps
- The Factorial
- Divide and Conquer
4Some 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
51. Reuse Dynamic Code
demo1_dynamicAutoexec.sas
61. Reuse Dynamic Code
The Greeting Macro
71. 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.
81. Reuse Dynamic Code
Macro Variable Dictionary
91. Reuse Dynamic Code
Macro Variable Dictionary
101. Reuse Dynamic Code
Delete Variables
112. Automated Process
Final report
Process parameters
Sas data sets
Proc tabulate
Create sample data
Sas data set
Task 2
Task 2
Task 1 obs1
Task 1
Task 1
Data step reads log
Sas log
Route log file to disk
Task 2 obs1
122. Automated Process
132. Automated Process
Using The Benchmark Macro
demo2_runBenchmark.sas
142. Automated Process
The Benchmark Macro
152. 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.
162. Automated Process
The Benchmark Macro
172. 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.
182. Automated Process
The Benchmark Macro
192. 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.
203. Automated Job Control
demo3_demostrateJobControl.sas
213. Automated Job Control
The Jobcontrol Macro
223. 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.
234. Extend SQL
demo4_surrogateKeys.sas
244. Extend SQL
The Nextval Macro
254. 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.
264. Extend SQL
The Nextval Macro
274. 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.
285. Pure Macros
without data steps
demo5_QueryDescriptor.sas
295. Pure Macros
The obsnvars Macro
305. 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.
316. Recursive Programming
http//www.mantasoft.co.uk/_stuff/Recursive.htm
n!
n! gamma(n1)
demo6_Factorial.sas
327. Divide Conquer
Not Sorted
Not Sorted
SORT
SORT
Sorted
Sorted
demo7_DivideAndConquer.sas
337. Divide Conquer
The actual input data
347. Divide Conquer
demo7_DivideAndConquer.log
357. 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.
367. Divide Conquer
PROC SORT TIME -gt 02220.658
SORT MACRO TIME -gt 00921.227
A SAVING OF 01259 (58)
377. Divide Conquer
The SORT macro
387. 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 dsnn will
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.
397. Divide Conquer
The SORT macro
1 Dataset having a length of less than MAXREC
Split Dataset into Several Sub-datasets While Sort
ing
407. Divide Conquer
The SORT macro
417. Divide Conquer
The SORT macro
42How 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
43How 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
44How To Learn More
- Technical Support
- http//support.sas.com/techsup/intro.html
- http//support.sas.com/techsup/faq/macro.html
45How To Learn More
- Books
- SAS Guide to Macro Processing 56041
- SAS Macro Language Reference 55501
- SAS Macro Facility Tips Techniques 55097
- Carpenters Complete Guide to the SAS Macro
Language 56100
46Questions ?