Sample 43052: Convert variables containing only numeric values from type character to numeric
This sample demonstrates how to check all values for all variables and those containing only numerics are converted to type numeric.
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.
/* sample data set where B and D have no alpha characters so they can be converted to numeric */
data test;
input a $ b $ c $ d $;
datalines;
1 2 4 5
20 33 44 2
a 4 23 5
2 9 cat 9
;
run;
/* use PROC TRANSPOSE to put each variable's values on one row */
proc transpose data=test out=new;
var a b c d;
run;
/* Put all variables into an array and use ANYALPHA function to */
/* determine if any row has a non-digit character and if so, */
/* increment CNT by 1. After all variables have been evaluated, */
/* if CNT is 0 that means there wasn't a non-digit so this */
/* variable name is put into a macro variable and when reading */
/* the last observation, put the total value of C into &TOT */
/* which tells you how many variables can be converted. This is */
/* used in the next step. */
data new2;
set new end=last;
array h(4) $ col1-col4;
cnt=0;
do i=1 to 4;
if anyalpha(h(i)) then cnt+1;
end;
if cnt=0 then do;
c+1;
call symputx('m'||left(put(c,8.)),_name_);
end;
if last then call symputx('tot',c);
run;
/* Let a macro %DO loop build the RENAME= data set option to rename */
/* all variables in macro variables to temporary names. This allows */
/* the original variable names to be used when converting from */
/* character to numeric variables with the INPUT function. */
%macro test;
data rename(drop=new1-new&tot);
array new(&tot)$ new1-new&tot;
set test(rename=(
%do i=1 %to &tot;
&&m&i=new&i
%end; ));
%do j=1 %to &tot;
&&m&j=input(new&j,8.);
%end;
run;
proc print;
run;
%mend test;
/* invoke the macro */
%test
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 newly created data set looks as it did when it was created but */
/* variables B and D are now numeric. */
/* partial output from PROC CONTENTS */
Alphabetic List of Variables and Attributes
# Variable Type Len
1 a Char 8
3 b Num 8
2 c Char 8
4 d Num 8
Type: | Sample |
Topic: | SAS Reference ==> DATA Step
|
Date Modified: | 2011-11-18 14:10:04 |
Date Created: | 2011-04-22 12:53:57 |
Operating System and Release Information
SAS System | Base SAS | z/OS | 9 TS M0 | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9 TS M0 | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9 TS M0 | |
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9 TS M0 | |
Microsoft Windows 2000 Advanced Server | 9 TS M0 | |
Microsoft Windows 2000 Datacenter Server | 9 TS M0 | |
Microsoft Windows 2000 Server | 9 TS M0 | |
Microsoft Windows 2000 Professional | 9 TS M0 | |
Microsoft Windows NT Workstation | 9 TS M0 | |
Microsoft Windows Server 2003 Datacenter Edition | 9 TS M0 | |
Microsoft Windows Server 2003 Enterprise Edition | 9 TS M0 | |
Microsoft Windows Server 2003 Standard Edition | 9 TS M0 | |
Microsoft Windows XP Professional | 9 TS M0 | |
64-bit Enabled AIX | 9 TS M0 | |
64-bit Enabled HP-UX | 9 TS M0 | |
64-bit Enabled Solaris | 9 TS M0 | |
HP-UX IPF | 9 TS M0 | |
Linux | 9 TS M0 | |
OpenVMS Alpha | 9 TS M0 | |
Tru64 UNIX | 9 TS M0 | |