## How to convert many character variables to numeric

Solved
Frequent Contributor
Posts: 130

# How to convert many character variables to numeric

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
Solution
‎05-24-2014 07:06 PM
PROC Star
Posts: 8,165

## Re: How to convert many character variables to numeric

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;

All Replies
Solution
‎05-24-2014 07:06 PM
PROC Star
Posts: 8,165

## Re: How to convert many character variables to numeric

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;

Frequent Contributor
Posts: 130

## Re: How to convert many character variables to numeric

Thank you, Arthur! I will study the code and to see how to apply it to my data.

Frequent Contributor
Posts: 130

## Re: How to convert many character variables to numeric

:vars separated by " "

SAS says  "missing a comma at the end of above statement".

Problem solved. Thank you,  Arthur and Patrick!

PROC Star
Posts: 8,165

## Re: How to convert many character variables to numeric

Glad to see that you figured it out by yourself.  I just added the missing comma to my original post

Frequent Contributor
Posts: 130

Thanks!

Posts: 4,736

## Re: How to convert many character variables to numeric

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?

Frequent Contributor
Posts: 130

## Re: How to convert many character variables to numeric

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".

Posts: 4,736

## Re: How to convert many character variables to numeric

Removed because of incorrect statement.

PROC Star
Posts: 8,165

## Re: How to convert many character variables to numeric

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.

🔒 This topic is solved and locked.