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

Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Embed Size (px)

Citation preview

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

Macro Variable ResolutionMacro Variable Resolution

Enio PresuttoEnio PresuttoYork University, Toronto, York University, Toronto, CanadaCanada

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

Page 3: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Simple Macro Resolution

• After a macro variable is created you can reference the variable as follows:

• &test• %put &test;• returns the value one

Page 4: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

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

Page 5: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Assigning digits

• %let numa=123;• %let numb=100+200;• %put &numa;• returns -- 123• %put &numb;• returns -- 100+200

Page 6: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Arithmetic Expressions

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

Page 7: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Assigning a null value

• %let status=;• %put &status;• returns --

Page 8: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

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

Page 9: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

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

Page 10: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

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

Page 11: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Direct Referencing

• %let dsn1=year1991;• %let dsn2=year1992;• %let dsn3=year1993;• %let dsn4=year1994;• %let dsn5=year1995;

Page 12: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Indirect Referencing

• %macro test;%do I=1 %to 5;

%put &dsn&I;

%put year199&I;

• %end;• %test;

Page 13: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

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

Page 14: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

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

Page 15: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

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>

Page 16: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

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”

Page 17: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

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”

Page 18: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

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

Page 19: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

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.

Page 20: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Resolving the Problem

• %macro enio;• proc sql ;• create table vmac as• select * from dictionary.macros;• quit;• run;

Page 21: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Results

• GLOBAL SQLRC 0• GLOBAL DSN year1995

Page 22: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Sample Program

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

Page 23: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Sample Program

• if _n_ eq 1 then• do;• fnd_dsn0='N';• fnd_dsn1='N';• end;

Page 24: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Sample Program

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

Page 25: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Sample Program

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

Page 26: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Sample Program

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

Page 27: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

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

Page 28: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

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;

Page 29: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

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

Page 30: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

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

Page 31: Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Copyrights

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