16
Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

Embed Size (px)

Citation preview

Page 1: Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

Session 7, Chapter 6

From SAS 9.3 Macro Language Reference

Macro Expressions P 71

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

Page 3: Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

Macro Language Operators – p 73Table 6.3

• P 73• ** • +• - • -^~ • etc

Page 4: Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

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;

Page 5: Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

%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. ;

Page 6: Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

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

Page 7: Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

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

Page 8: Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

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?

Page 9: Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

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.

Page 10: Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

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.

Page 11: Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

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.

Page 12: Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

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?;

Page 13: Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

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

Page 14: Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

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

Page 15: Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

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

Page 16: Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71

End

End