BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Emma2021
Quartz | Level 8
How can I change all character variables into numeric without changing variables names? Thank you
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

 

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

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.

Oligolas
Barite | Level 11

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 -

Tom
Super User Tom
Super User

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;

 

ballardw
Super User

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 4230 views
  • 4 likes
  • 4 in conversation