I have a data set that consists of names, addresses, state, zip, and phone number.
I am trying to convert the states from their two letter abbreviations to the full name of the state.
I tried using the stnamel function but I am not sure how to do it for multiple states;
For example, when I use
State=stnamel('PA');
put state;
State=stname ('CA')l;
put state;
All states in the State column turn to Pennsylvania.
I am a newbie to SAS and could not find examples of this.
Hi @klj81 does this not work for you?
data customers10;
length statename $50;
input First $ Last $ City $ State $ Zip;
statename=stnamel(state);
datalines;
Tyler Smith Upland PA 91783
Samantha Davis Montclair NJ 07163
Michael Rodriguez Metuchen NJ 08840
David Jorge Frenso CA 93650
;
run;
Hi:
You didn't show all your code or your data structure, so I'm not sure what you mean by "how to do it for multiple states" -- every row has a different state. As an example:
data state;
length state_name $50;
infile datalines;
input name $ stcode $;
state_name = stnamel(stcode);
putlog _n_= name= stcode= state_name=;
return;
datalines;
Alan NC
Barb SC
Cyndi OH
Dave DC
Eddy WA
;
run;
Results in this in the log:
You might mean that you have multiple states for every observation? Perhaps you have something like this:
which just means you have a function call for every variable.
Hope this helps,
Cynthia
Thanks for the response Cynthia!
I'll enter some of my code/data to try to explain a little better....
data customers10;
input First $ Last $ City $ State $ Zip;
datalines;
Tyler Smith Upland PA 91783
Samantha Davis Montclair NJ 07163
Michael Rodriguez Metuchen NJ 08840
David Jorge Frenso CA 93650
;
run;
I am looking to get the full name for the states like 'California' but every time I have tried the stnamel function I think I have done it incorrectly.
Hi @klj81 does this not work for you?
data customers10;
length statename $50;
input First $ Last $ City $ State $ Zip;
statename=stnamel(state);
datalines;
Tyler Smith Upland PA 91783
Samantha Davis Montclair NJ 07163
Michael Rodriguez Metuchen NJ 08840
David Jorge Frenso CA 93650
;
run;
By any chance are you having issues when the City is something like San Francisco?
Your example data step code would be reading Francisco into state and not CA.
Please show your entire data step. One line of code does not indicate when a value is assigned in relation to when it is written to a data set.
Some example input data may also be needed.
@klj81 wrote:
I have a data set that consists of names, addresses, state, zip, and phone number.
I am trying to convert the states from their two letter abbreviations to the full name of the state.
I tried using the stnamel function but I am not sure how to do it for multiple states;
For example, when I use
State=stnamel('PA');
put state;
State=stname ('CA')l;<= There is a typo on this line
put state;
All states in the State column turn to Pennsylvania.
I am a newbie to SAS and could not find examples of this.
The code you posted also would have an error for 'CA'.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.