BookmarkSubscribeRSS Feed
lawye010
Calcite | Level 5

Hello,

This is probably a very basic question but I was handed a large dataset set that does not have the variables numerically coded. I have imported the file but would like to relabel the variables with numerical codes. For instance for sex, right now the categories are male and female but I would like male=0 and female=1. How would I code this? Thanks.

3 REPLIES 3
Reeza
Super User

It depends. How many variables do you have to do this for? How many different options....and most importantly, why?

Some applications require that you do this prior to your analysis for example, but SAS does not and can handle variables that are categorical in an analysis properly. 

 

Some methods to fix this include:

  • Using custom informats and re-read the dat from the text file as needed
  • Using formats to remap the values to codes
  • Apply formats, changes how the variables appear but not the underlying data itself. Usually this is done in reverse, values are stored as numerics but displayed as text with full descriptions. Helpful for ensuring no typos and that the order is as desired
  • IF/THEN or SELECT statements to recode the values. 

If you need to do the same remapping for multiple variables you may also want to use arrays.

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 


@lawye010 wrote:

Hello,

This is probably a very basic question but I was handed a large dataset set that does not have the variables numerically coded. I have imported the file but would like to relabel the variables with numerical codes. For instance for sex, right now the categories are male and female but I would like male=0 and female=1. How would I code this? Thanks.


 

tarheel13
Rhodochrosite | Level 12

proc format;

value sexf

0="Male"

1="Female"

;

run;

 

data dsn;

set dsn;

format sex sexf.;

run;

ballardw
Super User

It may shortcut some question and answer cycles by running Proc Contents on your data set in question and sharing the result.

 

Another bit that may help is what do expect to do with 0/1 coding for sex that the current will not support?

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
  • 809 views
  • 0 likes
  • 4 in conversation