macro variable resolution enio presutto york university, toronto, canada
TRANSCRIPT
Macro Variable ResolutionMacro Variable Resolution
Enio PresuttoEnio PresuttoYork University, Toronto, York University, Toronto, CanadaCanada
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 dsn1=year1991;• %let dsn2=year1992;• %let dsn3=year1993;• %let dsn4=year1994;• %let dsn5=year1995;
Indirect Referencing
• %macro test;%do I=1 %to 5;
%put &dsn&I;
%put year199&I;
• %end;• %test;
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;
• Returnsdsn1 = year1991
dsn2 = year1992
dsn3 = year1993
dsn4 = year1994
dsn5 =
year1991 year1992 year1993 year1994 year1995 year1995
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
Sample HTML
• <select name=“dsn” size=“2” multiple>• <options value=“year1991”>year1991• <options value=“year1992”>year1992• <options value=“year1993”>year1993• <options value=“year1994”>year1994• </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” = “year1994”
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”
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;
• Returnsyear1994
year1995
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.
Resolving the Problem
• %macro enio;• proc sql ;• create table vmac as• select * from dictionary.macros;• quit;• run;
Results
• GLOBAL SQLRC 0• GLOBAL DSN year1995
Sample Program
• data _null_;• length valuea $ 200;• length fnd_dsn0 fnd_dsn1 $ 1;• retain fnd_dsn0 fnd_dsn1 valuea;• set vmac end=eof;
Sample Program
• if _n_ eq 1 then• do;• fnd_dsn0='N';• fnd_dsn1='N';• end;
Sample Program
• if name = upcase('dsn') then• do;• call symput("ndsn",left(value));• valuea=value;• end;
Sample Program
• if name = upcase('dsn0') then• do;• fnd_dsn0='Y';• call symput("ndsn0",left(value));• end;
Sample Program
• if name = upcase('dsn1') then• do;• fnd_dsn1='Y';• call symput("ndsn1",left(value));• end;
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));
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;
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
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
Copyrights
• The SAS system and SAS/IntrNet are registered trademarks of The SAS Institute Inc, Cary North Carolina, U.S.A.