Macro Variable Resolution Enio Presutto York University Toronto
Macro Variable Resolution Enio Presutto York University, Toronto, Canada
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
Simple Macro Resolution • After a macro variable is created you can reference the variable as follows: • &test • %put &test; • returns the value one
Assigning Text Constants • • • %let addr=maple; %let addr= maple ; %put &addr; returns value - maple leading and trailing blanks not stored quotation marks if included become part of the value
Assigning digits • • • %let numa=123; %let numb=100+200; %put &numa; returns -- 123 %put &numb; returns -- 100+200
Arithmetic Expressions • • • %let numb=%eval(100+200); %put &numb; returns -- 300 %let numb=%sysevalf(124+. 242); %put &numb; returns -- 124. 242
Assigning a null value • %let status=; • %put &status; • returns --
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
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
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
Direct Referencing • • • %let %let dsn 1=year 1991; dsn 2=year 1992; dsn 3=year 1993; dsn 4=year 1994; dsn 5=year 1995;
Indirect Referencing • %macro test; %do I=1 %to 5; %put &dsn&I; %put year 199&I; • %end; • %test;
Indirect Referencing • %macro test; %do I=1 %to 5; %put dsn&I =year 199&I; %let dsn&I=year 199&I; • %end; • %put &dsn 1 &dsn 2 &dsn 3 &dsn 4 &dsn 5; • %mend; • %test; • Returns dsn 1 = year 1991 dsn 2 = year 1992 dsn 3 = year 1993 dsn 4 = year 1994 dsn 5 = year 1991 year 1992 year 1993 year 1994 year 1995
SAS and the Web • HTML form contains a pull down from which the user can select 1 or many options • SAS/Intr. Net broker sends this information via Macro Variables
Sample HTML • • • <select name=“dsn” size=“ 2” multiple> <options value=“year 1991”>year 1991 <options value=“year 1992”>year 1992 <options value=“year 1993”>year 1993 <options value=“year 1994”>year 1994 </select>
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” = “year 1994”
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” = “year 1994” – “dsn 0”= “ 2” – “dsn 1”=“year 1994” – “dsn 2”=“year 1995”
Determining what has been sent %let dsn 0=2; %let dsn 1=year 1994; %let dsn 2=year 1995; %macro test; %do i=1 %to &dsn 0; %put &&dsn&i; %end; %mend; %test; • Returns year 1994 year 1995
Determining what has been sent %let dsn=year 1994; %macro test; %do i=1 %to &dsn 0; %put &&dsn&i; %end; %mend; %test; • Returns • WARNING: Apparent symbolic reference DSN 0 not resolved. • ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &DSN 0 • ERROR: The %TO value of the %DO I loop is invalid. • ERROR: The macro will stop executing.
Resolving the Problem • • • %macro enio; proc sql ; create table vmac as select * from dictionary. macros; quit; run;
Results • GLOBAL SQLRC DSN 0 year 1995
Sample Program • • • data _null_; length valuea $ 200; length fnd_dsn 0 fnd_dsn 1 $ 1; retain fnd_dsn 0 fnd_dsn 1 valuea; set vmac end=eof;
Sample Program • if _n_ eq 1 then • do; • fnd_dsn 0='N'; • fnd_dsn 1='N'; • end;
Sample Program • if name = upcase('dsn') then • do; • call symput("ndsn", left(value)); • valuea=value; • end;
Sample Program • if name = upcase('dsn 0') then • do; • fnd_dsn 0='Y'; • call symput("ndsn 0", left(value)); • end;
Sample Program • if name = upcase('dsn 1') then • do; • fnd_dsn 1='Y'; • call symput("ndsn 1", left(value)); • end;
Sample Program • if name = upcase('dsn 2') then call symput("ndsn 2", left(value)); • if name = upcase('dsn 3') then call symput("ndsn 3", left(value)); • if name = upcase('dsn 4') then call symput("ndsn 4", left(value)); • if name = upcase('dsn 5') then call symput("ndsn 5", left(value)); • if name = upcase('dsn 6') then call symput("ndsn 6", left(value));
Sample Program • • • if eof then do; if fnd_dsn 0 eq 'N' and fnd_dsn 1 eq 'N' then do; call symput("ndsn 0", left('1')); call symput("ndsn 1", left(trim(valuea))); end; 3 run %mend;
Sample Program • • • %let dsn=year 1994; %macro test; %enio; %do i=1 %to &ndsn 0; %put &&ndsn&i; %end; %mend; %test; run; • Returns • GLOBAL NDSN 0 1 • GLOBAL NDSN year 1994 • GLOBAL NDSN 1 year 1994
Sample Program • • • %let dsn 0=2; %let dsn 1=year 1994; %let dsn 2=year 1995; ; %macro test; %enio; %do i=1 %to &ndsn 0; %put &&ndsn&i; %end; %mend; %test; run; • Returns • GLOBAL NDSN 0 2 • GLOBAL NDSN year 1994 • GLOBAL NDSN 1 year 1994 • GLOBAL NDSN 2 year 1995
Copyrights • The SAS system and SAS/Intr. Net are registered trademarks of The SAS Institute Inc, Cary North Carolina, U. S. A.
- Slides: 31