Presentation is loading. Please wait.

Presentation is loading. Please wait.

Macro Variable Resolution Enio Presutto York University, Toronto, Canada.

Similar presentations


Presentation on theme: "Macro Variable Resolution Enio Presutto York University, Toronto, Canada."— Presentation transcript:

1 Macro Variable Resolution Enio Presutto York University, Toronto, Canada

2 Creating Macro Variables %let test=one; test is the name of the macro variable one is the value of the macro variable test value assigned can contain letters, numbers, printable characters and blanks

3 Simple Macro Resolution After a macro variable is created you can reference the variable as follows: &test %put &test; returns the value one

4 Assigning Text Constants %let addr=maple; %put &addr; returns value - maple leading and trailing blanks not stored quotation marks if included become part of the value

5 Assigning digits %let numa=123; %let numb=100+200; %put &numa; returns -- 123 %put &numb; returns -- 100+200

6 Arithmetic Expressions %let numb=%eval(100+200); %put &numb; returns -- 300 %let numb=%sysevalf(124+.242); %put &numb; returns -- 124.242

7 Assigning a null value %let status=; %put &status; returns --

8 Assigning Macro Variable References %let comp=York University; %let addr=4700 keele st; %let who=&comp &addr, North York; %put &who; returns -- York University 4700 keele st, North York

9 Assigning special characters %let double=%str(one two); %put &double returns -- %let poss=%str(Enio%’s Place); &double returns -- one two &poss returns Enio’s Place

10 Scope Gobal - –exist for duration of SAS Session –can be referenced anywhere in the program, inside or outside macros Local –exist only during execution of the macro in which the variable is created

11 Direct Referencing %let dsn1=year1991; %let dsn2=year1992; %let dsn3=year1993; %let dsn4=year1994; %let dsn5=year1995;

12 Indirect Referencing %macro test; %do I=1 %to 5; %put &dsn&I; %put year199&I; %end; %test;

13 Indirect Referencing %macro test; %do I=1 %to 5; %put dsn&I =year199&I; %let dsn&I=year199&I; %end; %put &dsn1 &dsn2 &dsn3 &dsn4 &dsn5; %mend; %test; Returns dsn1 = year1991 dsn2 = year1992 dsn3 = year1993 dsn4 = year1994 dsn5 = year1991 year1992 year1993 year1994 year1995 year1995

14 SAS and the Web HTML form contains a pull down from which the user can select 1 or many options SAS/IntrNet broker sends this information via Macro Variables

15 Sample HTML year1991 year1992 year1993 year1994

16 Macro Variables Sent to Server If user select 1 option then server is sent the macro variable dsn and it will be assigned the value the user selects Symbols passed to SAS –#symbols: 2 ” –_debug" = "131" –“dsn” = “year1994”

17 Macro Variables Sent to Server If user select multiple options then server is sent the following: Symbols passed to SAS –#symbols: 2 ” –_debug" = "131" –“dsn” = “year1994” –“dsn0”= “2” –“dsn1”=“year1994” –“dsn2”=“year1995”

18 Determining what has been sent %let dsn0=2; %let dsn1=year1994; %let dsn2=year1995; %macro test; %do i=1 %to &dsn0; %put &&dsn&i; %end; %mend; %test; Returns year1994 year1995

19 Determining what has been sent %let dsn=year1994; %macro test; %do i=1 %to &dsn0; %put &&dsn&i; %end; %mend; %test; Returns WARNING: Apparent symbolic reference DSN0 not resolved. ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &DSN0 ERROR: The %TO value of the %DO I loop is invalid. ERROR: The macro will stop executing.

20 Resolving the Problem %macro enio; proc sql ; create table vmac as select * from dictionary.macros; quit; run;

21 Results GLOBAL SQLRC 0 GLOBAL DSN year1995

22 Sample Program data _null_; length valuea $ 200; length fnd_dsn0 fnd_dsn1 $ 1; retain fnd_dsn0 fnd_dsn1 valuea; set vmac end=eof;

23 Sample Program if _n_ eq 1 then do; fnd_dsn0='N'; fnd_dsn1='N'; end;

24 Sample Program if name = upcase('dsn') then do; call symput("ndsn",left(value)); valuea=value; end;

25 Sample Program if name = upcase('dsn0') then do; fnd_dsn0='Y'; call symput("ndsn0",left(value)); end;

26 Sample Program if name = upcase('dsn1') then do; fnd_dsn1='Y'; call symput("ndsn1",left(value)); end;

27 Sample Program if name = upcase('dsn2') then call symput("ndsn2",left(value)); if name = upcase('dsn3') then call symput("ndsn3",left(value)); if name = upcase('dsn4') then call symput("ndsn4",left(value)); if name = upcase('dsn5') then call symput("ndsn5",left(value)); if name = upcase('dsn6') then call symput("ndsn6",left(value));

28 Sample Program if eof then do; if fnd_dsn0 eq 'N' and fnd_dsn1 eq 'N' then do; call symput("ndsn0",left('1')); call symput("ndsn1",left(trim(valuea))); end; end;3 run %mend;

29 Sample Program %let dsn=year1994; %macro test; %enio; %do i=1 %to &ndsn0; %put &&ndsn&i; %end; %mend; %test; run; Returns GLOBAL NDSN0 1 GLOBAL NDSN year1994 GLOBAL NDSN1 year1994

30 Sample Program %let dsn0=2; %let dsn1=year1994; %let dsn2=year1995;; %macro test; %enio; %do i=1 %to &ndsn0; %put &&ndsn&i; %end; %mend; %test; run; Returns GLOBAL NDSN0 2 GLOBAL NDSN year1994 GLOBAL NDSN1 year1994 GLOBAL NDSN2 year1995

31 Copyrights The SAS system and SAS/IntrNet are registered trademarks of The SAS Institute Inc, Cary North Carolina, U.S.A.


Download ppt "Macro Variable Resolution Enio Presutto York University, Toronto, Canada."

Similar presentations


Ads by Google