BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Jonate_H
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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;

View solution in original post

9 REPLIES 9
art297
Opal | Level 21

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;

Jonate_H
Quartz | Level 8

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

Jonate_H
Quartz | Level 8

:vars separated by " "

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


Problem solved. Thank you,  Arthur and Patrick!

art297
Opal | Level 21

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

Patrick
Opal | Level 21

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?

Jonate_H
Quartz | Level 8

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

Patrick
Opal | Level 21

Removed because of incorrect statement.

art297
Opal | Level 21

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 9 replies
  • 1379 views
  • 6 likes
  • 3 in conversation