%do i = 1 %to &word_cnt;
%let temp=%qscan(%bquote(&varlist),&i);
Generate the code that will be returned to the RENAME option. It adds an underscore to
each word creating syntax, for example, age=_age.
&temp = _&temp
End the %DO block.
%end;
End the macro.
%mend rename;
Use the RENAME = option in the SET statement to invoke the macro that renames each
variable in the macro variable named &VARLIST. The %UNQUOTE function is used to
remove the masking placed on the values using the %QSCAN function.
data new;
set sashelp.class(rename=(%unquote(%rename)));
run;
Use PROC PRINT to display the results.
proc print;
run;
Output
Obs _Name _Sex _Age _Height _Weight
1 Alfred M 14 69.0 112.5
2 Alice F 13 56.5 84.0
3 Barbara F 13 65.3 98.0
4 Carol F 14 62.8 102.5
5 Henry M 14 63.5 102.5
6 James M 12 57.3 83.0
Example 11: Loop through Dates Using a Macro
%DO Loop
Details
This macro illustrates how to loop through a starting and ending date by month.
Program
%macro date_loop(start,end);
%let start=%sysfunc(inputn(&start,anydtdte9.));
%let end=%sysfunc(inputn(&end,anydtdte9.));
%let dif=%sysfunc(intck(month,&start,&end));
%do i=0 %to &dif;
Example 11: Loop through Dates Using a Macro %DO Loop 467
%let date=%sysfunc(intnx(month,&start,&i,b),date9.);
%put &date;
%end;
%mend date_loop;
%date_loop(01jul2015,01feb2016)
Program Description
Begin the macro definition with two parameters.
%macro date_loop(start,end);
Use the %LET statement to change the value of the macro variable named &START.
The INPUTN function is used so that the ANYDTDTE informat can be used on the
value that is passed to &START. Using the ANYDTDTE informat causes the value of
&START to be a SAS formatted date.
%let start=%sysfunc(inputn(&start,anydtdte9.));
Use the %LET statement to change the value of the macro variable named &END. The
INPUTN function is used so that the ANYDTDTE informat can be used on the value
that is passed to &END. Using the ANYDTDTE informat causes the value of &END to
be a SAS formatted date.
%let end=%sysfunc(inputn(&end,anydtdte9.));
Use the %LET statement to create a macro variable named &DIF. The INTCK function
is used to return the number of months between &START and &END. The MONTH
function is used as the first argument to retrieve the month interval. When using this
function within the macro facility, quotation marks are not used around MONTH, like
you would in the DATA step.
%let dif=%sysfunc(intck(month,&start,&end));
The %DO statement is used to loop through the number of months (&DIF) between
&START and &END.
%do i=0 %to &dif;
Use the %LET statement to create a macro variable named &DATE. The INTNX
function is used to increment the &START date by MONTH. The fourth argument, B,
specifies the alignment. The
B argument specifies that the returned date or datetime
value is aligned to the beginning of the interval. The second argument to %SYSFUNC is
the format DATE9, which is applied to the value that is returned from the function
INTNX. The %PUT statement is used to write the date value to the log.
%let date=%sysfunc(intnx(month,&start,&i,b),date9.);
%put &date;
End the %DO block.
%end;
End the macro.
%mend date_loop;
Invoke the macro by passing the starting date and the ending date parameters.
%date_loop(01jul2015,01feb2016)
468 Appendix 5 SAS Macro Examples
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset