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!
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;
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;
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.
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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
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.