![]() | ![]() | ![]() | ![]() | ![]() |
This sample program 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 sample program can be accessed under the Full Code tab.
The first DATA step creates a sample data set of sport league team names, team colors, and scores of three games. The desired result is to create new variables using each team color concatenated with the team name and the word "Total" to output a single observation assigning each new variable the coordinating total sum of the three game scores.
The second step is a DATA _NULL_ step used along with CALL SYMPUTX 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 called COUNT to count the total number of observations read. In the CALL SYMPUTX the name of each macro variable will have the COUNT value appended to the end of name in order to create macro variables called MACVAR1, MACVAR2, etc. 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 called MAX that contains the final COUNT value.
The third step requires the DATA step to be inside a macro in order to assign the separate macro variables their appropriate total using a %DO loop. When the do loop index variable 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. 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 final IF statement is used to check when at the end of the data set to output the single observation.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
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
;
data _null_;
set teams end=end;
count+1;
call symputx('macvar'||left(count),compress(color)||compress(team_name)||"Total");
if end then call symputx('max',count);
run;
options mprint;
%macro newvars;
data teamscores;
set teams end=end;
%do i = 1 %to &max;
if _n_=&i then do;
&&macvar&i=sum(of game1-game3);
retain &&macvar&i;
keep &&macvar&i;
end;
%end;
if end then output;
%mend;
%newvars
proc print noobs;
title "League Team Game Totals";
run;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
League Team Game Totals
Blue Red
Green Sea Yellow Hot Purple
Crickets Otters Stingers Ants Cats
Total Total Total Total Total
25 23 28 26 27
| Type: | Sample |
| Topic: | SAS Reference ==> Macro SAS Reference ==> DATA Step |
| Date Modified: | 2012-02-13 15:40:47 |
| Date Created: | 2012-02-10 09:02:30 |
| Product Family | Product | Host | SAS Release | |
| Starting | Ending | |||
| SAS System | Base SAS | Aster Data nCluster on Linux x64 | ||
| DB2 Universal Database on AIX | ||||
| DB2 Universal Database on Linux x64 | ||||
| Greenplum on Linux x64 | ||||
| Netezza TwinFin 32bit blade | ||||
| Netezza TwinFin 32-bit SMP Hosts | ||||
| Netezza TwinFin 64-bit S-Blades | ||||
| Netezza TwinFin 64-bit SMP Hosts | ||||
| Teradata on Linux | ||||
| z/OS | ||||
| Z64 | ||||
| OpenVMS VAX | ||||
| Microsoft® Windows® for 64-Bit Itanium-based Systems | ||||
| Microsoft Windows Server 2003 Datacenter 64-bit Edition | ||||
| Microsoft Windows Server 2003 Enterprise 64-bit Edition | ||||
| Microsoft Windows XP 64-bit Edition | ||||
| Microsoft® Windows® for x64 | ||||
| OS/2 | ||||
| Microsoft Windows 95/98 | ||||
| Microsoft Windows 2000 Advanced Server | ||||
| Microsoft Windows 2000 Datacenter Server | ||||
| Microsoft Windows 2000 Server | ||||
| Microsoft Windows 2000 Professional | ||||
| Microsoft Windows NT Workstation | ||||
| Microsoft Windows Server 2003 Datacenter Edition | ||||
| Microsoft Windows Server 2003 Enterprise Edition | ||||
| Microsoft Windows Server 2003 Standard Edition | ||||
| Microsoft Windows Server 2003 for x64 | ||||
| Microsoft Windows Server 2008 | ||||
| Microsoft Windows Server 2008 for x64 | ||||
| Microsoft Windows XP Professional | ||||
| Windows 7 Enterprise 32 bit | ||||
| Windows 7 Enterprise x64 | ||||
| Windows 7 Home Premium 32 bit | ||||
| Windows 7 Home Premium x64 | ||||
| Windows 7 Professional 32 bit | ||||
| Windows 7 Professional x64 | ||||
| Windows 7 Ultimate 32 bit | ||||
| Windows 7 Ultimate x64 | ||||
| Windows Millennium Edition (Me) | ||||
| Windows Vista | ||||
| Windows Vista for x64 | ||||
| 64-bit Enabled AIX | ||||
| 64-bit Enabled HP-UX | ||||
| 64-bit Enabled Solaris | ||||
| ABI+ for Intel Architecture | ||||
| AIX | ||||
| HP-UX | ||||
| HP-UX IPF | ||||
| IRIX | ||||
| Linux | ||||
| Linux for x64 | ||||
| Linux on Itanium | ||||
| OpenVMS Alpha | ||||
| OpenVMS on HP Integrity | ||||
| Solaris | ||||
| Solaris for x64 | ||||
| Tru64 UNIX | ||||




