Help using Base SAS procedures

How to read from end of string?

Reply
Contributor
Posts: 28

How to read from end of string?

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
Frequent Contributor
Posts: 102

Re: How to read from end of string?

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.

Respected Advisor
Posts: 3,156

Re: How to read from end of 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

;

Super User
Posts: 10,044

Re: How to read from end of string?


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

Ask a Question
Discussion stats
  • 3 replies
  • 216 views
  • 0 likes
  • 4 in conversation