Sample 37433: Rename each variable in a data set by adding a prefix
The sample code that is illustrated on the Full Code tab uses the %SYSFUNC function along with certain SAS® functions to retrieve the variable names. We then use the RENAME option to rename each variable. A prefix is added to each variable. This prefix is passed to the macro as a parameter.
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.
The macro code below uses the %SYSFUNC function along with certain SAS
® functions to retrieve the variable names. We then use the RENAME option to rename each variable. A prefix is added to each variable. This prefix is passed to the macro as a parameter.
/** Create a test data set **/
data one;
input t_iba t_iet t_ica t_www t_itt t_gga;
datalines;
1 2 3 4 5 6
;
run;
%macro vars(dsn,chr,out);
%let dsid=%sysfunc(open(&dsn));
%let n=%sysfunc(attrn(&dsid,nvars));
data &out;
set &dsn(rename=(
%do i = 1 %to &n;
%let var=%sysfunc(varname(&dsid,&i));
&var=&chr&var
%end;));
%let rc=%sysfunc(close(&dsid));
run;
%mend vars;
/** First parameter is the data set that contains all of the variables. **/
/** Second parameter is the characters used for the prefix. **/
/** Third parameter is the new data set that contains the new variables. **/
%vars(one,SP,final1)
proc print;
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.
Obs SPt_iba SPt_iet SPt_ica SPt_www SPt_itt SPt_gga
1 1 2 3 4 5 6
This macro enables you to pass in the data set name and the prefix that you want each variable to begin with.
Date Modified: | 2012-06-18 10:32:20 |
Date Created: | 2009-10-12 14:03:23 |
Operating System and Release Information
SAS System | Base SAS | z/OS | | |
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 7 | | |
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 2008 | | |
Microsoft Windows XP Professional | | |
Windows Millennium Edition (Me) | | |
Windows Vista | | |
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 | | |