BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
klj81
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisBrooks
Ammonite | Level 13

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;

View solution in original post

5 REPLIES 5
Cynthia_sas
SAS Super FREQ

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:

stnamel.png

You might mean that you have multiple states for every observation? Perhaps you have something like this:

mult_var.png

which just means you have a function call for every variable.

 

Hope this helps,

Cynthia

klj81
Fluorite | Level 6

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.

 

 

ChrisBrooks
Ammonite | Level 13

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;
ballardw
Super User

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.

ballardw
Super User

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'.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 6882 views
  • 3 likes
  • 4 in conversation