BookmarkSubscribeRSS Feed
brush01
Calcite | Level 5

I'm working with the MTCars data set in SAS Studio, and noticed the cylinder column is of numeric type. I feel that because the number of cylinders in a car is either 4, 6, or 8, a data-type of categorical would be more appropriate. (I have included a screenshot of my issue.)

 

https://prnt.sc/1woaj4u

 

How do I change a data-type when working with a data set?

3 REPLIES 3
Reeza
Super User

You don't need to change the type of the variable. 

Depending on which procedure you're using, you can include your variable in the CLASS statement and SAS will treat it as categorical variable. You can recode it to a character but it's unnecessary, categorical, nominal, or ordinal variables can be numeric or character in SAS. 

 

Ordinal logistic model example:

proc logistic data=sashelp.cars;
class cylinders / param=ref;
model origin = cylinders;
run;

@brush01 wrote:

I'm working with the MTCars data set in SAS Studio, and noticed the cylinder column is of numeric type. I feel that because the number of cylinders in a car is either 4, 6, or 8, a data-type of categorical would be more appropriate. (I have included a screenshot of my issue.)

 

https://prnt.sc/1woaj4u

 

How do I change a data-type when working with a data set?


 

Reeza
Super User
If you do want to convert it, SAS requires it to be a new variable so something like below:

data cars;
set sashelp.cars;
Cylinder_character = put(cylinders, 8. -l);
run;
tarheel13
Rhodochrosite | Level 12

you can use if-else statements if you really want a categorical variable. 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 5074 views
  • 0 likes
  • 3 in conversation