This sample shows how to convert all character variables to numeric while excluding one character variable and keeping the same variable names in the output data set.
In general, a list of all the character variables will be used to create three macro variables. One contains the list of character variable names, the second contains the list of new numeric variable names, and the third contains the list of new numeric variables names set equal to the list of character variable names for the RENAME statement. The SQL procedure is used to create the macro variables and the DATA step is used to convert the values from character to numeric.
Click the Full Code tab to view the sample program and further details about the process.
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 sample data set TEST contains both character and numeric variables */
data test;
input id $ b c $ d e $ f;
datalines;
AAA 50 11 1 222 22
BBB 35 12 2 250 25
CCC 75 13 3 990 99
;
run;
/* PROC CONTENTS is used to create an output data set called VARS to list all */
/* variable names and their type from the TEST data set. */
proc contents data=test out=vars(keep=name type) noprint;
run;
/* A DATA step is used to subset the VARS data set to keep only the character */
/* variables and exclude the one ID character variable. A new list of numeric */
/* variable names is created from the character variable name with a "_n" */
/* appended to the end of each name. */
data vars;
set vars;
if type=2 and name ne 'id';
newname=trim(left(name))||"_n";
run;
/* The macro system option SYMBOLGEN is set to be able to see what the macro */
/* variables resolved to in the SAS log. */
options symbolgen;
/* PROC SQL is used to create three macro variables with the INTO clause. One */
/* macro variable named C_list will contain a list of each character variable */
/* separated by a blank space. The next macro variable named N_list will */
/* contain a list of each new numeric variable separated by a blank space. The */
/* last macro variable named Renam_list will contain a list of each new numeric */
/* variable and each character variable separated by an equal sign to be used in */
/* the RENAME statement. */
proc sql noprint;
select trim(left(name)), trim(left(newname)),
trim(left(newname))||'='||trim(left(name))
into :c_list separated by ' ', :n_list separated by ' ',
:renam_list separated by ' '
from vars;
quit;
/* The DATA step is used to convert the numeric values to character. An ARRAY */
/* statement is used for the list of character variables and another ARRAY for */
/* the list of numeric variables. A DO loop is used to process each variable */
/* to convert the value from character to numeric with the INPUT function. The */
/* DROP statement is used to prevent the character variables from being written */
/* to the output data set, and the RENAME statement is used to rename the new */
/* numeric variable names back to the original character variable names. */
data test2;
set test;
array ch(*) $ &c_list;
array nu(*) &n_list;
do i = 1 to dim(ch);
nu(i)=input(ch(i),8.);
end;
drop i &c_list;
rename &renam_list;
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.
Type: | Sample |
Topic: | Common Programming Tasks ==> Selecting Data Common Programming Tasks ==> Working with Character Data Data Management ==> Manipulation and Transformation ==> Array processing Data Management ==> Manipulation and Transformation SAS Reference ==> DATA Step |
Date Modified: | 2010-08-27 11:09:58 |
Date Created: | 2010-08-26 12:59:46 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | 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 |