The sample code on the Full Code tab illustrates how to add a suffix or prefix to a group of variables in a data set. It can be used to rename all, or a subset, of the variables.
.
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.
There are three examples of PROC SQL code with Example 1 uncommented. To use another section, comment out Example 1 and uncomment the desired section. You need to choose one of the three examples of PROC SQL code to run and then run the PROC DATASETS and PROC CONTENTS steps.
/* Create test data */
data one;
input id name :$10. age score1 score2 score3;
datalines;
1 George 10 85 90 89
2 Mary 11 99 98 91
3 John 12 100 100 100
4 Susan 11 78 89 100
;
run;
/* Use PROC SQL to create a macro variable that contains the list of variables to be renamed. */
/* There are 3 examples of PROC SQL statements here to generate macro variables. */
/* Whichever one you choose, you will need to modify the libname and memname (data set name) */
/* values accordingly for your data set. The values need to be in upper case. */
/* Example 1: */
/* This code creates a macro variable &list with the list of variables in the form. */
/* id = id_OLD */
/* This format could be used to add a suffix to all the variables. */
proc sql noprint;
select cats(name,'=',name,'_OLD')
into :list
separated by ' '
from dictionary.columns
where libname = 'WORK' and memname = 'ONE';
quit;
/* Example 2: */
/* This code creates a macro variable &list in the form */
/* id=OLD_id */
/* This form could be used to add a prefix to all the variables. */
/*
proc sql noprint;
select cats(name,'=','OLD_',name)
into :list
separated by ' '
from dictionary.columns
where libname = 'WORK' and memname = 'ONE';
quit;
*/
/* Example 3:*/
/* This code subsets the variables to be renamed by adding to the WHERE clause. */
/* In this case, choose only the variables that start with SCORE. */
/*
proc sql noprint;
select cats(name,'=',name,'_OLD')
into :list
separated by ' '
from dictionary.columns
where libname = 'WORK' and memname = 'ONE'
and upcase(name) like 'SCORE%';
quit;
*/
/* Use PROC DATASETS to do the rename using the macro variable you have created. */
/* Modify the libref and data set name for your data set. */
proc datasets library = work nolist;
modify one;
rename &list;
quit;
/* Verify the changes with a PROC CONTENTS on your data set. */
proc contents data = work.one;
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.
Alphabetic List of Variables and Attributes # Variable Type Len 3 age_OLD Num 8 1 id_OLD Num 8 2 name_OLD Char 10 4 score1_OLD Num 8 5 score2_OLD Num 8 6 score3_OLD Num 8
Type: | Sample |
Topic: | SAS Reference ==> Statements ==> Information ==> RENAME |
Date Modified: | 2019-07-15 08:58:43 |
Date Created: | 2012-12-13 09:06:16 |
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 8 Pro | ||||
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 Server 2012 | ||||
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 |