BookmarkSubscribeRSS Feed
lhinrichs
Calcite | Level 5

Hello, I would appreciate some assistance in converting variables that have imported as categorical to numeric. I intend to drop the variables read as categorical, and keep the name of the variable for the numeric variable. Thank you!

3 REPLIES 3
ballardw
Super User

Examples of the values involved will help.

 

Generally I suggest going back to how the data was read and modify the approach to ensure the desired results start as intended. Several of the reasons that data that should be numeric end up as character also cutoff the values and may mean that converting the character values are not actually what is wanted.

PaigeMiller
Diamond | Level 26

Agreeing with @ballardw , the best approach is to fix the way the variables are created in the first place, it seems as if something is broken there.

 

Variables cannot be converted from categorical to numeric (without some trickery). Easiest to create a new numeric variable with a new name that will hold the numeric values.

 

data example;
     charvar = '123548';
     numvar = input(charvar, 8.);
run;

 

 

Version with trickery:

 

data have;
    vaccuum='123548';
run;
data want;
    set have(rename=(vaccuum=vaccuum_old));
    vaccuum = input(vaccuum_old, 8.);
    drop vaccuum_old;
run;

 

--
Paige Miller
Astounding
PROC Star

SAS doesn't contain categorical variables.  It contains only numeric variables and character variables.  Certainly it is possible to have variables that contain only a limited number of values.  But they are still either numeric or character once they are imported.

 

Give a specific example of what you intend as the before and after result, and you are sure to get further guidance.

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!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1450 views
  • 0 likes
  • 4 in conversation