160 likes | 275 Vues
This guide provides an overview of the SAS macro language, focusing on macro expressions, evaluation methods, and logical comparisons. It covers key functions like %EVAL, %SYSEVALF, and the use of operators for arithmetic and logical expressions. Learn how to create reusable macros and handle different data types including integers and floating points. The guide also explains string handling techniques and macro quoting methods such as %STR, %BQUOTE, and %SUPERQ for effective macro variable utilization in your SAS programs.
E N D
Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71
&Eval - p 72-73 %let A=2; %let B=5; %let operator=+; %put The result of &A &operator &B is %eval(&A &operator &B).; The result of 2 + 5 is 7.
Macro Language Operators – p 73Table 6.3 • P 73 • ** • + • - • -^~ • etc
The macro facility is a string handling facility – p 74-75 * Use %EVAL in a %LET statement; %let a=%eval(1+2); %let b=%eval(10*3); %let c=%eval(4/2); %let i=%eval(5/3); %put The value of a is &a; %put The value of b is &b; %put The value of c is &c; %put The value of I is &i; ******** Note answers are all integer;
%SYSEVAL p. 75 * USE SYSEVAL INSTEAD; %let a=%sysevalf(10.0*3.0); %let b=%sysevalf(10.5+20.8); %let c=%sysevalf(5/3); %put 10.0*3.0 = &a; %put 10.5+20.8 = &b; %put 5/3 = &c; * Note answers can be floating point now. ;
How Macro Evaluates Logical Expression p 76 %macro compnum(first,second); %if &first>&second %then %put &first is greater than &second; %else %if &first=&second %then %put &first equals &second; %else %put &first is less than &second; %mend compnum; *Invoke the COMPNUM macro with these values; %compnum(1,2) %compnum(-1,0) ; The following results are displayed in the log: 1 is less than 2 -1 is less than 0
Other comparisons • Floating point p 76-77 • Type in the code on top of 77 & test • Compare character operands in logical expressions p 77 • Type in example code middle of p 77 & test
Chapter 7 – Macro Quoting – p 79 • Why doesn’t this work? P 81 %let print=proc print; run;; • Why does this work? %let print=%str(proc print; run;); • What does %str() do?
NRSTR%() - P 88 • Use NRSTR%() when there is a macro variable name in the expression that you don’t want evaluated – this prevents the macro variable from being evaluated %macro example; %local myvar; %let myvar=abc; %put %nrstr(The string &myvar appears in log output,); %put instead of the variable value.; %mend example; %example • The string &myvar appears in log output, • instead of the variable value.
Example 2 %NRSTR • Type in example middle of p 88 %macro credits(d=%nrstr(Mary&Stacy&Joan Ltd.)); footnote "Designed by &d"; %mend credits; • Run with %credits() • Observe output. What happens if you use %SRT instead? Try it.
BQuote p 89 • Use BQUOTE() when an expression has unmatching quotes such as Jones’s • NRBQUOTE() when there is a macro expression you don’t want evaluated + unmatching quotes.
Example BQUOTE – p 90 ** Example BQWUOTE p 90; data test; store="Susan's Office Supplies"; call symput('s',store); run; %macro readit; %if %bquote(&s) ne %then %put *** valid ***; %else %put *** null value ***; %mend readit; %readit ; * Observe output in log – what if you don’t use BQUOTE?;
SuperQ – p 92 ***********EXAMPLE SUPERQ ********AND %WINDOW; * It masks items that might require macro quoting; %window ask #5 @5 'Enter two values:' #5 @24 val 15 attr=underline; %macro a; %put *** This is a. ***; %mend a; %macro test; %display ask; %put *** %superq(val) ***; /* Note absence of ampersand */ %mend test; *invoke the macro TEST; %test
Ch 9, 10 & 11 • Ch 9 - Storing and Reusing Macros – read on your own • Ch 10 – Macro Errors • Note bullets on p 121 • See Note bottom p 129 double and single quotes • Ch 11 – Writing Efficient Code - read on your own
Ch 12 – Macro Language Elements • Review Table 12.1 p 156 • Table 12.2 p 157 • Table 12.3 p 158 • Table 12.4 p 159 • Table 12.5 p 160 • Table 12.9 p 166
End End