A More Involved Macro Application
10. August 2007 10:19
Example: Generate several SAS dataset names.
Call Execute Using a Macro Define a macro to do most of the work, then call it.
Resolves an argument and executes the resolved value at step boundary. This is a very powerful tool can can use the power of the data step to generate code.
Resolves the value of a text expression during DATA step execution. Syntax: variable=RESOLVE(argument);
%MACRO DSNAMES(PREFIX,FIRST,LAST); %LOCAL N; %DO N=&FIRST %TO &LAST; &PREFIX&N %END; %MEND DSNAMES; data %dsnames(count,1,4) ; . . . Generates: data count1 count2 count3 count4;
SAS DATA Step Interfaces
- SYMGET, SYMPUT, and other interfaces using macro variables can transfer values between SAS steps.
- SYMGET returns macro variable values to the DATA step
- Macro variables created with SYMPUT, can be referenced via & in the NEXT step.
proc means nway median data=sample;
where team_nbr ne 1 ;
var ttl_income_neto1 ;
class team_nbr;
output out=res median=income_median;
run;
data k;
set res;
length m $1;
team=put(team_nbr,1.) ;
run;
data _null_;
set k;
call symput('norma'||team,income_median) ;
run;
data sample1;
set sample;
length m $1;
team=put(team_nbr,1.) ;
run;
data sample3 ;
set sample;
norma=symget('norma'||team);
target=(ttl_income_neto1 ge symget('norma'||team));
run;CALL SYMPUTX
SYMPUTX will automatically convert and trim numbers.
Example:
%LET SAMPSIZE=5;
DATA SAMPLE;
DO N=1 TO &SAMPSIZE;
OBSNO=CEIL(UNIFORM(0)*TOTOBS);
SET POP POINT=OBSNO NOBS=TOTOBS;
OUTPUT;
END;
CALL SYMPUTX('MTOTOBS',TOTOBS);
STOP;
RUN;CALL EXECUTE:
Call Execute Using a Macro Define a macro to do most of the work, then call it.
Resolves an argument and executes the resolved value at step boundary. This is a very powerful tool can can use the power of the data step to generate code.
%macro printcty(mcountynm,mctyobs);
PROC PRINT DATA=PERM.COUNTYDT;
SUGI 31 Hands-on Workshops
WHERE COUNTYNM="&mcountynm";
OPTIONS PAGENO=1;
TITLE "REPORT FOR COUNTY &mcountynm";
FOOTNOTE "TOTAL OBSERVATION COUNT WAS &MCTYOBS";
RUN;
%mend printcty;
data pass2;
set perm.countydt;
by countynm;
if first.countynm then ctyobs=0;
ctyobs+1;
if last.countynm then
call execute('%printcty(' !!
countynm !!
',' !!
put(ctyobs,2.) !!
')'
);
run;The RESOLVE FunctionResolves the value of a text expression during DATA step execution. Syntax: variable=RESOLVE(argument);
%let event=Holiday;
%macro mdate;
SUGI 31 Hands-on Workshops
4th of July
%mend mdate;
data test;
length var1-var3 $ 15;
when='%mdate';
var1=resolve('&event'); /* macro variable reference */
var2=resolve('%mdate'); /* macro invocation */
var3=resolve(when); /* DATA step var with macro call */
put '*** ' var1= var2= var3=;
run;
*** var1=Holiday var2=4th of July var3=4th of July
Email 