How to transfer character variables into numeric variables by

How to transfer character variables into numeric variables by

Post by angelina.a » Mon, 25 Jul 2005 17:57:04


I have some mixed columns in an Excel data. So I read all variables into
SAS as character variables. Now I want to transfer all character variables
to numerical variables, but how should I write the programI wrote the
following program, but it doesn't work. Can anybody tell me where is wrong
or how to write correctly

data chen;
set chen;
array XXX[*] _character_;
do kk=1 to DIM(XXX);
XXX[kk]=input(XXX[kk],1.);
end;
run;


Thanks~~
 
 
 

How to transfer character variables into numeric variables by

Post by xiangyang. » Mon, 25 Jul 2005 23:56:17

Hi Chiu-chen,

If the variables number in your dataset is not too many, you can try
the following method:

data temp;
input id $ x $ y $;
cards;
1001 2 3
1002 1 5
1003 1 1
;
run;
proc sql noprint;
create table temp2 as
select id,
input(x,8.) as x format=best12. informat=12. length=8,
input(y,8.) as y format=best12. informat=12. length=8 from temp;
quit;

Best regards.

Sean

 
 
 

How to transfer character variables into numeric variables by

Post by art29 » Tue, 26 Jul 2005 00:22:27

Would the following do what you want?

data chen;
input a $ b $ c $;
cards;
1 2 3
a 4 5
6 b 7
8 c d
;
run;
data chen;
set chen;
array XXX[*] _character_;
array YYY[3];
do kk=1 to DIM(XXX);
YYY[kk]=input(XXX[kk],1.);
end;
run;

Art
---------
On Sun, 24 Jul 2005 04:57:04 -0400, subscribe sas-L Chen, Chiu-Chen
 
 
 

How to transfer character variables into numeric variables by

Post by tobydun » Tue, 26 Jul 2005 00:47:00

Chiu-Chen,

There is no way easy way to get SAS to do what you want. The problem will
coms in since you have some vars that are true character variables, some
that are true numerics held as characters, and here is the problem some that
are a mixture of character and numeric and right fully held as character.
If I understand you correctly you have all vars as character. Thus any mass
indescriminant conversion will give you errors in your log and not a good
error at that.

To do what you want all you need is a few simple things, first a list of the
numeric variables held as text that you want converted to numeric, second a
rename statement, length statement,, a drop statement on th eoutput side of
things, and lastly an assignment statement.

data one ;
infile cards dsd dlm = ' ' ;
input x1 $ x2 $ x3 $ x4 $ ;
cards ;
A 1 1 1
B 2 B 2
C 3 C 3
D 4 4 4
;
run ;

data two (drop = _:) ;
length x2 x4 8. ;
set one (rename = (x2 = _x2 x4 = _x4)) ;

x2 = _x2 ;
x4 = _x4 ;

run ;

proc contents ;
run ;



Now if you have a lot of vars you need converted this process can be made
easier by the use of either macros and/or arays.




Toby Dunn




From: "subscribe sas-L Chen, Chiu-Chen" < XXXX@XXXXX.COM >
Reply-To: "subscribe sas-L Chen, Chiu-Chen" < XXXX@XXXXX.COM >
To: XXXX@XXXXX.COM
Subject: How to transfer character variables into numeric variables by
using DO LOOP
Date: Sun, 24 Jul 2005 04:57:04 -0400

I have some mixed columns in an Excel data. So I read all variables into
SAS as character variables. Now I want to transfer all character variables
to numerical variables, but how should I write the programI wrote the
following program, but it doesn't work. Can anybody tell me where is wrong
or how to write correctly

data chen;
set chen;
array XXX[*] _character_;
do kk=1 to DIM(XXX);
XXX[kk]=input(XXX[kk],1.);
end;
run;


Thanks~~
 
 
 

How to transfer character variables into numeric variables by

Post by jim1sta » Tue, 26 Jul 2005 15:35:17

Hi Chen,

I have a macro that does that, while "preserving" original variable names
and attributes like labels. You can specify more than one variable at a
time, while you may also specify no-numeric values and their corresponding
new numeric values to be replaced by.
See: http://www.yqcomputer.com/
and in the ZIP file the .SAS macro.

Regards - Jim.
My computer always teaches me something new I thought I knew already.

[common disclaimer]

On Sun, 24 Jul 2005 04:57:04 -0400, subscribe sas-L Chen, Chiu-Chen