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

I have a zip code variable in a dataset that includes both 5- and 9-digit zip codes. I want all of them to be 5-digits but am having trouble extracting the first 5 digits of the variable. It is an extensive list, but some examples are 15009, 15208, 191451652, 193760024.

 

When I try to use substr,

 

data cases; set cases;

zip=substr(zip_char,1,5);

run;

 

it seems to truncate the zip codes that are already 5 characters long to "1". Any thoughts on how to avoid this?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

This would happen if ZIP_CHAR is not left-hand justified.  Luckily, it's easy to left-hand justify:

 

length zip $ 5;

zip = left(zip_char);

 

SUBSTR isn't really needed, once you have assigned a length to the new variable.

View solution in original post

3 REPLIES 3
LinusH
Tourmaline | Level 20
Perhaps your zip_char is right aligned.
Try to do left() prior to substr().
Data never sleeps
Astounding
PROC Star

This would happen if ZIP_CHAR is not left-hand justified.  Luckily, it's easy to left-hand justify:

 

length zip $ 5;

zip = left(zip_char);

 

SUBSTR isn't really needed, once you have assigned a length to the new variable.

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