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
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;
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;
Thanks!
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.