If you really wanted a simple way to convert all character variables to numeric then here is a simply way.
Get a list of the names of the variables. If the list is short enough you could just put it into a macro variable.
proc sql noprint;
select nliteral(name) into :varlist separated by ' '
from dictionary.columns
where libname='WORK'
and memname='HAVE'
and type='char'
;
quit;
Now use that list of names to write the values to a file. Then read the values back as numbers.
filename text temp;
data _null_;
set have;
file text dsd ;
put &varlist;
run;
data want;
set have(drop=&varlist);
infile text dsd truncover ;
input &varlist;
run;
You can't. But you can change the name back.
Here is how you could change one.
data want;
set have;
num = input(char,32.);
drop char ;
rename num=char;
run;
For multiple you might adapt this to this pattern.
data want;
set have;
num1 = input(char1,32.);
num2 = input(char2,32.);
drop char1 char2 ;
rename num1=char1 num2=char2;
run;
If you have the list of the names of the character variable then it should be simple enough to generate the code from the names.
Hi,
try this:
%MACRO convertChar2Num(varlist=);
%local varlist i currItem;
%LET i=1;
%LET currItem=%SCAN(&varlist.,&i.,%STR( ));
%DO %WHILE(%LENGTH(&currItem.)>0);
_hlp&currItem.=input(&currItem.,best32.);
if strip(&currItem.) ne strip(put(_hlp&currItem.,best32.)) then put 'E' 'RROR: unexpected different values encountered, please check!' _N_= &currItem.= @100 _hlp&currItem.=;
drop &currItem.;
rename _hlp&currItem.=&currItem.;
%put &=varlist &=i &=currItem;
%LET i=%EVAL(&i.+1);
%LET currItem=%SCAN(&varlist.,&i.,%STR( ));
%END;
%MEND convertChar2Num;
data class;
set sashelp.class;
x=put(age,best.);
y=put(weight,best.);
z=put(height,best.);
drop age weight height;
%convertChar2Num(varlist=x y z);
run;
- Cheers -
If you really wanted a simple way to convert all character variables to numeric then here is a simply way.
Get a list of the names of the variables. If the list is short enough you could just put it into a macro variable.
proc sql noprint;
select nliteral(name) into :varlist separated by ' '
from dictionary.columns
where libname='WORK'
and memname='HAVE'
and type='char'
;
quit;
Now use that list of names to write the values to a file. Then read the values back as numbers.
filename text temp;
data _null_;
set have;
file text dsd ;
put &varlist;
run;
data want;
set have(drop=&varlist);
infile text dsd truncover ;
input &varlist;
run;
@Emma2021 wrote:
How can I change all character variables into numeric without changing variables names? Thank you
The real question is why are variables that should be numeric character in the first place. Most of the time when we see this question it relates to not taking control of your data early enough, as in when it is read into SAS and typically involves one or more of 1) spreadsheet data, 2) poorly laid out - multiple header rows for example or values like NA or NULL in 'numeric' columns, and 3) relying on Proc Import or a wizard.
Usually this means that the data should be read with a data step where you control things.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.
Ready to level-up your skills? Choose your own adventure.