End the DO loop block.
end;
run;
End the macro.
%mend pname;
Invoke the macro.
%pname
Use the %PUT statement to print the contents of &PGMNAME to the log.
%put pgmname=&pgmname;
Example 7: Using a Macro to Create New Variable
Names from V
ariable Values
Details
This example shows how to create new variable names using the values of existing
variables. Macro processing is required to store the values within macro variables, and
then assign each new variable the desired value in a subsequent DATA step. The desired
result is to create new variables using each team color concatenated with the team name
and the word Total. And output a single observation assigning each new variable the
coordinating total sum of the three game scores.
Program
data teams;
input color $15. @16 team_name $15. @32 game1 game2 game3;
datalines;
Green Crickets 10 7 8
Blue Sea Otters 10 6 7
Yellow Stingers 9 10 9
Red Hot Ants 8 9 9
Purple Cats 9 9 9
;
%macro newvars(dsn);
data _null_;
set &dsn end=end;
count+1;
call symputx('macvar'||left(count),compress(color)||compress(team_name)||"Total");
if end then call symputx('max',count);
run;
data teamscores;
set &dsn end=end;
%do i = 1 %to &max;
if _n_=&i then do;
458 Appendix 5 SAS Macro Examples
&&macvar&i=sum(of game1-game3);
retain &&macvar&i;
keep &&macvar&i;
end;
%end;
if end then output;
%mend newvars;
%newvars(teams)
proc print noobs;
title "League Team Game Totals";
run;
Program Description
Create the data set.
data teams;
input color $15. @16 team_name $15. @32 game1 game2 game3;
datalines;
Green Crickets 10 7 8
Blue Sea Otters 10 6 7
Yellow Stingers 9 10 9
Red Hot Ants 8 9 9
Purple Cats 9 9 9
;
Begin the macro definition with one parameter.
%macro newvars(dsn);
Use the DATA _NULL_ step along with CALL SYMPUTX call routine to create a
separate macro variable for each observation read from the Teams data set. The END=
option is used in the SET statement to create a variable that indicates when the end of
file is reached. A counter is created in a variable named COUNT to count the total
number of observations read. In the CALL SYMPUTX call routine the name of each
macro variable will have the COUNT value appended to the end of name. This routine
creates macro variables named &MACVAR1, &MACVAR2, and so on. The values
passed to each macro variable is the concatenation of the COLOR, TEAM_NAME, and
the word Total. The COMPRESS function is used to remove any blank spaces in the
COLOR or TEAM_NAME value since SAS variable names cannot contain blanks. The
last IF condition creates a single macro variable named &MAX that contains the final
COUNT value.
data _null_;
set &dsn end=end;
count+1;
call symputx('macvar'||left(count),compress(color)||compress(team_name)||"Total");
if end then call symputx('max',count);
run;
Create a new data set named TeamScores and use the SET statement to bring in the data
set that is passed to the macro.
data teamscores;
set &dsn end=end;
Example 7: Using a Macro to Create New Variable Names from Variable Values 459
Use the %DO statement to loop through the total number of observations (&MAX).
%do i = 1 %to &max;
When the %DO loop index variable &I matches the DATA step iteration variable _N_
the new variable for the current COLOR and TEAM_NAME is assigned the sum of the
GAME1-GAME3 variables using the SUM function. This is done by using macro
indirect referencing. When you use an indirect macro variable reference, you force the
macro processor to scan the macro variable reference more than once and resolve the
desired reference on the second, or later, scan. To force the macro processor to rescan a
macro variable reference, you use more than one ampersand in the macro variable
reference. When the macro processor encounters multiple ampersands, its basic action is
to resolve two ampersands to one ampersand. The RETAIN statement is needed to retain
the values of the new variables throughout the DATA step in order to output them all on
a single observation. The KEEP statement is used to output only the new variables to the
new data set. The %END statement closes the %DO block. The final IF statement is
used to check for the ending of the data set and outputs the single observation.
if _n_=&i then do;
&&macvar&i=sum(of game1-game3);
retain &&macvar&i;
keep &&macvar&i;
end;
%end;
if end then output;
End the macro.
%mend newvars;
Invoke the macro using the name of the data set containing the variables to change.
%newvars(teams)
Use PROC PRINT to print the results of the data set TeamScores.
proc print noobs;
title "League Team Game Totals";
run;
460 Appendix 5 SAS Macro Examples
Log
MPRINT(NEWVARS): data _null_;
MPRINT(NEWVARS): set teams end=end;
MPRINT(NEWVARS): count+1;
MPRINT(NEWVARS): call
symputx('macvar'||left(count),compress(color)||compress(team_name)||"Total");
MPRINT(NEWVARS): if end then call symputx('max',count);
MPRINT(NEWVARS): run;
NOTE: Numeric values have been converted to character values at the places given
by:
(Line):(Column).
863:77
NOTE: There were 5 observations read from the data set WORK.TEAMS.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
MPRINT(NEWVARS): data teamscores;
MPRINT(NEWVARS): set teams end=end;
MPRINT(NEWVARS): if _n_=1 then do;
MPRINT(NEWVARS): GreenCricketsTotal=sum(of game1-game3);
MPRINT(NEWVARS): retain GreenCricketsTotal;
MPRINT(NEWVARS): keep GreenCricketsTotal;
MPRINT(NEWVARS): end;
MPRINT(NEWVARS): if _n_=2 then do;
MPRINT(NEWVARS): BlueSeaOttersTotal=sum(of game1-game3);
MPRINT(NEWVARS): retain BlueSeaOttersTotal;
MPRINT(NEWVARS): keep BlueSeaOttersTotal;
MPRINT(NEWVARS): end;
MPRINT(NEWVARS): if _n_=3 then do;
MPRINT(NEWVARS): YellowStingersTotal=sum(of game1-game3);
MPRINT(NEWVARS): retain YellowStingersTotal;
MPRINT(NEWVARS): keep YellowStingersTotal;
MPRINT(NEWVARS): end;
MPRINT(NEWVARS): if _n_=4 then do;
MPRINT(NEWVARS): RedHotAntsTotal=sum(of game1-game3);
MPRINT(NEWVARS): retain RedHotAntsTotal;
MPRINT(NEWVARS): keep RedHotAntsTotal;
MPRINT(NEWVARS): end;
MPRINT(NEWVARS): if _n_=5 then do;
MPRINT(NEWVARS): PurpleCatsTotal=sum(of game1-game3);
MPRINT(NEWVARS): retain PurpleCatsTotal;
MPRINT(NEWVARS): keep PurpleCatsTotal;
MPRINT(NEWVARS): end;
MPRINT(NEWVARS): if end then output;
Output
Blue Red
Green Sea Yellow Hot Purple
Crickets Otters Stingers Ants Cats
Total Total Total Total Total
25 23 28 26 27
Example 7: Using a Macro to Create New Variable Names from Variable Values 461
..................Content has been hidden....................

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