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

I have a long list of zip codes that is a string of unformatted numeric numbers. I need to format them with a hyphen (re: zip+4) and if the zip only has 5 digits, I need a 0 in front it as well.

 

Data:

obs      zip 

1           985051536

2          65132022

3           217718034

 

I want:

obs      zip 

1           98505-1536

2          06513-2022

3           21771-8034

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

data have;
input obs      zip : $20.;
cards;
1           985051536
2          65132022
3           217718034
;

proc format;
picture fmt
low-high='99999-9999'
;
run;
data want;
 set have;
 want=input(zip,best32.);
 format want fmt.;
run;

View solution in original post

4 REPLIES 4
Ksharp
Super User

data have;
input obs      zip : $20.;
cards;
1           985051536
2          65132022
3           217718034
;

proc format;
picture fmt
low-high='99999-9999'
;
run;
data want;
 set have;
 want=input(zip,best32.);
 format want fmt.;
run;
whymath
Lapis Lazuli | Level 10
Impressive!
ChickenLittle
Obsidian | Level 7

Thanks!

whymath
Lapis Lazuli | Level 10

If it is not easy to design such a format, you can use character functions:

data have;
  input ip $42.; 
  cards;
985051536
65132022
217718034
;
run;

data want;
  set have;
  new_ip=ifc(length(ip)<9,'0',trimn(''))||ip;
  new_ip=substr(new_ip,1,5)||'-'||substr(new_ip,6);
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 365 views
  • 1 like
  • 3 in conversation