BookmarkSubscribeRSS Feed
sconn
Calcite | Level 5

I know you can change the lables for variables but how do I change record values? 

 

For example, I have a variable named state and then the abbreviations for 50 states, how can I change and label them to the full name?

Ex: CA = California 

7 REPLIES 7
Jagadishkatam
Amethyst | Level 16

There are different approaches to change the variable values, either you could try with if then else condition or simply using the format

 

example1: if then else

 

Data want;
set have;
if var='CA' then var1='California';
else if var='TX' then var1='Texas';
run;

example 2: with format, we need to create the format first and to the same variable we could apply that format with format statement.

proc format;
value $states
'CA'='California'
'TX'='Texas';
run;

data want;
set have;
format var $states.;
run;

Thanks,
Jag
sconn
Calcite | Level 5

thanks Jag,

 

I did this and no errors came up but it did not seem to change in the tables or dataset. Thank you for your help!

Jagadishkatam
Amethyst | Level 16
Could you please post your code or screenshot of the code.
Thanks,
Jag
sconn
Calcite | Level 5

proc format;
value $states
'MA'='Massachusetts'
'CT'='Connecticut'
'NH'='New Hampshire'
'VT'='Vermont'
'ME'='Maine'
'RI'='Rhode Island';
run;

data Final;
set Final3;
format var $states.;
run;

Jagadishkatam
Amethyst | Level 16
what is the variable in final3 dataset which has the values as MA,CT,NH. you have to mention that variable name in the format statement replacing the 'var' in data step.
Thanks,
Jag
Reeza
Super User

@sconn Your code appears correct but note that a format changes how a variable is displayed but not the underlying value. 

 

So, if it's not working, your starting point isn't necessarily what you think, IE CA isn't CA but something else. 

 

Look at a proc contents of your dataset before and after you apply the format and try the IF version. 

Post the proc contents if you need help debugging. 

Ksharp
Super User
Check STNAME(). 


state=stname('NC');
put state; NORTH CAROLINA
state=stnamel('NC');
put state; North Carolina



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