Hi I have a variable with different record lengths. I am trying to find out if i can read from the end of the string. Also, If any of the "want" is alphanumeric, then DELETE. I want only US based Zip, not canadian postal code
Have | Want |
---|---|
WARREN MI 48091-3241 | 48091 |
BLOOMFIELD MI 48304-2916 | 48304 |
SAINT CLAIR SHORES MI 48082-1158 | 48082 |
HUNTINGTON WOODS MI 48070 | 48070 |
MACOMB MI 48042-2398 | 48042 |
HUNTINGTN WDS , MI 480700000 | 48070 |
You can try INDEX() function to find the location of the first instance of a numeric character preceded by a blank (including the blank will prevent you from reading Canadian postal codes that begin with a letter) in your string and then nest it in SUBSTRN() function to read 5 characters to the right of the first number. Finally, sounds like you'll want to nest both of the first two in the INPUT() function to output a number rather than a character string.
data test;
infile cards truncover;
input str $100.;
num=prxchange('s/(^\D+)(\b\d{5})(.+)/$2/io',-1, str);
cards;
WARREN MI 48091-3241
BLOOMFIELD MI 48304-2916
SAINT CLAIR SHORES MI 48082-1158
HUNTINGTON WOODS MI 48070
MACOMB MI 48042-2398
HUNTINGTN WDS , MI 480700000
;
data test; infile cards truncover; input str $100.; length num $ 5; num=scan(str,-1,' '); cards; WARREN MI 48091-3241 BLOOMFIELD MI 48304-2916 SAINT CLAIR SHORES MI 48082-1158 HUNTINGTON WOODS MI 48070 MACOMB MI 48042-2398 HUNTINGTN WDS , MI 480700000 ; run;
Xia Keshan
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.