BookmarkSubscribeRSS Feed
gobejo
Calcite | Level 5

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

HaveWant
WARREN MI 48091-324148091
BLOOMFIELD MI 48304-291648304
SAINT CLAIR SHORES MI 48082-115848082
HUNTINGTON WOODS MI 4807048070
MACOMB MI 48042-239848042
HUNTINGTN WDS     , MI 48070000048070
3 REPLIES 3
Haris
Lapis Lazuli | Level 10

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.

Haikuo
Onyx | Level 15

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

;

Ksharp
Super User

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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 1026 views
  • 0 likes
  • 4 in conversation