BookmarkSubscribeRSS Feed
Smitha9
Fluorite | Level 6

Hi, 

I have a dataset which have character variables. I want all the variables converted to numeric together with a code. I want the code which makes all the variables to numeric together.

 

thank you!

9 REPLIES 9
Jagadishkatam
Amethyst | Level 16

 

 

data want;
set have;
array char(*) $ _character_;
array num(*) var1-Var10;

do i = 1 to dim(char);
if char(i) ne . then num(i)=input(char(i),best.);
end;
run;

Thanks,
Jag
Smitha9
Fluorite | Level 6
I have three character variables which needs to be numeric. pincode,dateofbirth and platenumber.
how could i put these 3variables in this code to convert all these to numeric?
Jagadishkatam
Amethyst | Level 16

Please try the below code for pincode and platenumber for conversion from character to numeric but for dateofbirth, we need to follow different approach inorder to read the date with a informat and then convert to numeric as in the code. Hope this helps

 

 

data want;
set have;
array char(*) $ pincode platenumber;
array num(*) pincoden platenumbern;
do i = 1 to dim(char);
if char(i) ne . then num(i)=input(char(i),best.);
end;
dateofbirth=input(dateofbirth,anydtdte.);
run;

Thanks,
Jag
Reeza
Super User
Did you import your data from a CSV or text file? If so, you should fix it at that stage and not after the fact. If you've read it in correctly from the source you can't correct it after the fact because you don't have it. Albeit, that's less likely when you've read them in as character field unless it was accidentally truncated.
Smitha9
Fluorite | Level 6
imported from csv file. how can i fix it in that stage? can you let me know? thanks
Reeza
Super User
When you used proc import the code was written to the log. Go find that code and modify the properties of the variables you want fixed. Apply the proper informat, format and length if needed.
ballardw
Super User

Often such a question relates to a problem or mistake made when bringing the data into SAS.

So, how did you read the data into SAS to begin with?

 

And ALL character variables or some variables?

Do you expect to have the same variable name when the process is finished? Once a SAS variable is created the type cannot change.

Smitha9
Fluorite | Level 6
most of them are character variables which are meant to be numeric
Astounding
PROC Star

This is a case where you can't afford to be lazy.  You have to actually examine the data.

 

SAS made these variables character for a reason.  Here are examples of values that might appear in the data, that SAS would have to treat as character:

 

> 5

N/A

+ 0

 

In real life, there will be many more examples.  You have to examine the data and decide what the right numeric value would be.  Here's how you do that:

 

proc freq data=have;

tables char_var;

where input(char_var, ??8.) = .;

run;

 

This will show you all the values that could not be converted to numeric.  Examine them, decide what the right "decision" would be about how to store them as numeric.  Then we can talk about programming.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 9 replies
  • 1095 views
  • 4 likes
  • 5 in conversation