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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1925 views
  • 0 likes
  • 4 in conversation