- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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".
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you, Arthur! I will study the code and to see how to apply it to my data.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
:vars separated by " "
SAS says "missing a comma at the end of above statement".
Problem solved. Thank you, Arthur and Patrick!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Glad to see that you figured it out by yourself. I just added the missing comma to my original post
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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".
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Removed because of incorrect statement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.