JC Raymond, Education, SAS Bill Fehlner, Education, SAS - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

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

Description:

Using the Macro Facility Programming Techniques in SAS: ... allows the macro processor to use data step functions directly and format the result. %sysevalf ... – PowerPoint PPT presentation

Number of Views:177
Avg rating:3.0/5.0
Slides: 47
Provided by: monsugCa
Category:

less

Transcript and Presenter's Notes

Title: JC Raymond, Education, SAS Bill Fehlner, Education, SAS


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_at_sas.com 416
307-4513 bill.fehlner_at_sas.com
2
(No Transcript)
3
Programming Examples
  1. Reuse dynamic code
  2. Automated Process
  3. Automate job control
  4. Extend SQL with custom functions
  5. Pure macros without data steps
  6. The Factorial
  7. Divide and Conquer

4
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

5
1. Reuse Dynamic Code
demo1_dynamicAutoexec.sas
6
1. Reuse Dynamic Code
The Greeting Macro
7
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.

8
1. Reuse Dynamic Code
Macro Variable Dictionary
9
1. Reuse Dynamic Code
Macro Variable Dictionary
10
1. Reuse Dynamic Code
Delete Variables
11
2. 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
12
2. Automated Process
13
2. Automated Process
Using The Benchmark Macro
demo2_runBenchmark.sas
14
2. Automated Process
The Benchmark Macro
15
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.

16
2. Automated Process
The Benchmark Macro
17
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.

18
2. Automated Process
The Benchmark Macro
19
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.

20
3. Automated Job Control
demo3_demostrateJobControl.sas
21
3. Automated Job Control
The Jobcontrol Macro
22
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.

23
4. Extend SQL
demo4_surrogateKeys.sas
24
4. Extend SQL
The Nextval Macro
25
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.

26
4. Extend SQL
The Nextval Macro
27
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.

28
5. Pure Macros
without data steps
demo5_QueryDescriptor.sas
29
5. Pure Macros
The obsnvars Macro
30
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.

31
6. Recursive Programming
http//www.mantasoft.co.uk/_stuff/Recursive.htm
n!
n! gamma(n1)
demo6_Factorial.sas
32
7. Divide Conquer
Not Sorted
Not Sorted
SORT
SORT
Sorted
Sorted
demo7_DivideAndConquer.sas
33
7. Divide Conquer
The actual input data
34
7. Divide Conquer
demo7_DivideAndConquer.log
35
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.

36
7. Divide Conquer
PROC SORT TIME -gt 02220.658
SORT MACRO TIME -gt 00921.227
A SAVING OF 01259 (58)
37
7. Divide Conquer
The SORT macro
38
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 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.

39
7. Divide Conquer
The SORT macro
1 Dataset having a length of less than MAXREC
Split Dataset into Several Sub-datasets While Sort
ing
40
7. Divide Conquer
The SORT macro
41
7. Divide Conquer
The SORT macro
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
  • 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

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

45
How 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

46
Questions ?
Write a Comment
User Comments (0)
About PowerShow.com