O'Reilly logo

SAS 9.4 Macro Language, 5th Edition by

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

End the macro.
%mend test;
Invoke the macro by using the name of the file to check for data.
%test(c:\test.txt)
Example 10: Retrieve Each Word from a Macro
Variable List
Details
This macro retrieves each word from a list, adds an underscore to the beginning of each
word, and then renames variables based on these new values.
Program
%let varlist = Age Height Name Sex Weight;
%macro rename;
%let word_cnt=%sysfunc(countw(&varlist));
%do i = 1 %to &word_cnt;
%let temp=%qscan(%bquote(&varlist),&i);
&temp = _&temp
%end;
%mend rename;
data new;
set sashelp.class(rename=(%unquote(%rename)));
run;
proc print;
run;
Program Description
Use the %LET statement to create a macro variable named &VARLIST that contains a
list of words. In this case the words are a list of variables that will be renamed.
%let varlist = Age Height Name Sex Weight;
Begin the macro definition.
%macro rename;
Use the %LET statement to create a macro variable named &WORD_CNT. Use the
COUNTW function to receive the number of words in the list.
%let word_cnt=%sysfunc(countw(&varlist));
Use a %DO statement to loop through the number of words specified in the
&WORD_CNT macro variable. Use the %LET statement to create a macro variable
named &TEMP that uses the %QSCAN function to mask and retrieve each word from
the macro variable &VARLIST. The %BQUOTE function is used to mask any special
characters that are in &VARLIST.
466 Appendix 5 SAS Macro Examples

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required