I have a data set, for some reason, there are 60 character variables (var1, var2, ... var60) which they should be numeric. How to convert them to numeric?
Those variable names don't follow any pattern, they are just random names, eg, var1's name is apple, var2's name is car... etc...
Use input(var, format) function to replace one by one is unrealistic.
There are some other variables which must remain as character, for example, organization name (which are letters ), and organization ID (which are numbers stored as character). For those 60 variables, they are numbers but somehow incorrectly stored as "character".
Here is one way:
/*create some test data*/
data have (drop=_:);
set sashelp.class (rename=(height=_height
weight=_weight));
height=put(_height, best12.);
weight=put(_weight, best12.);
run;
data variables;
length vname $32;
input vname;
cards;
height
weight
;
proc sql;
select catt(vname,"=_",vname),
catt("_",vname),
vname
into :renames separated by " ",
:vars separated by " ",
:vnames separated by " "
from variables
;
quit;
data want (drop=_:);
set have (rename=(&renames.));
array cvars $ &vars;
array vars &vnames.;
do over cvars;
vars=input(cvars,12.);
end;
run;
Here is one way:
/*create some test data*/
data have (drop=_:);
set sashelp.class (rename=(height=_height
weight=_weight));
height=put(_height, best12.);
weight=put(_weight, best12.);
run;
data variables;
length vname $32;
input vname;
cards;
height
weight
;
proc sql;
select catt(vname,"=_",vname),
catt("_",vname),
vname
into :renames separated by " ",
:vars separated by " ",
:vnames separated by " "
from variables
;
quit;
data want (drop=_:);
set have (rename=(&renames.));
array cvars $ &vars;
array vars &vnames.;
do over cvars;
vars=input(cvars,12.);
end;
run;
Thank you, Arthur! I will study the code and to see how to apply it to my data.
:vars separated by " "
SAS says "missing a comma at the end of above statement".
Problem solved. Thank you, Arthur and Patrick!
Glad to see that you figured it out by yourself. I just added the missing comma to my original post
Thanks!
Are there also character variables in this data set which must remain character? If so: Do we need to test first if a character variable contains only strings which can get converted or are there so few "real" character variables left that you could provide these names as input to the conversion process?
There are some other variables which must remain as character, for example, organization name (which are letters ), organization ID (which are numbers stored as character). For those 60 variables, they are numbers but somehow incorrectly stored as "character".
Removed because of incorrect statement.
My suggested is code to simply create a file that contains the names of all of the variables that need to be changed. The rest of the suggested code takes care of changing those variables from character to numeric.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.