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

Hello,

 

I have heard of the zipstate() function, but I am not sure how to first get my data in the correct structure to be able to use it. I want to add 2 columns to the following data, a new variable called zipcode, and a new one called state

.

So I want to figure out first how to extract the first zip, ie the first 5 digits from "zipcode_5"  ( we will use the first zipcode, even when there are multiple, since only the state will matter). and then create a state variable, that uses the newly created, single zipcode

 

I appreciate any help.

 

Data

Capture.JPG

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
state=zipstate(substr(zipcode_5,1,5));

The SUBSTR function in this case selects the first 5 characters.

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26
state=zipstate(substr(zipcode_5,1,5));

The SUBSTR function in this case selects the first 5 characters.

--
Paige Miller
Student77
Obsidian | Level 7

I understand this part, but as I stated, I first need to create a column that selects the first 5 digits in the zipcode_5 variable.

 

zipstate() does not work for those that have multiple zips, for ex, someone may have a "zipcode_5" of 77550_77555_77553, and zipstate() returns a blank for these.

 

So I need to first create a column called "zip" where it extracts the first 5 digits from zipcode_5 and then run zipstate on the new variable with single zipcodes.

Student77
Obsidian | Level 7

perfect thanks!

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
  • 3 replies
  • 1311 views
  • 0 likes
  • 2 in conversation