DATA Step, Macro, Functions and more

extract a substring from a string

Accepted Solution Solved
Reply
Contributor QLi
Contributor
Posts: 57
Accepted Solution

extract a substring from a string

All;

I have data as the following.

data  have;

input string $40.;

datalines;

c 10 year fixed

n 30 year fixed

n 15 year fixed

sh nchfa fha 30 year

c 30 year fixed homepath

c 7 yr arm homepath

;

run;

I want to substring from a string to get the column like:

10 year

30 year

15 year

30 year

7 yr

Thanks


Accepted Solutions
Solution
‎06-13-2012 10:21 PM
Super User
Posts: 9,662

Re: extract a substring from a string

Another PRX Function.

data  have;
input string $40.;
datalines;
c 10 year fixed
n 30 year fixed
n 15 year fixed
sh nchfa fha 30 year
c 30 year fixed homepath
c 7 yr arm homepath
;
run;
data want(drop=pid);
 set have;
 length year $ 20;
 retain pid;
 if _n_ eq 1 then pid=prxparse('/(\d+\s*(year|yr))/i');
 if prxmatch(pid,string) then year=prxposn(pid,1,string);
run; 

Ksharp

View solution in original post


All Replies
Respected Advisor
Posts: 4,641

Re: extract a substring from a string

The ideal candidate for PRX regular expressions :

data  have;

input string $40.;

datalines;

c 10 year fixed

n 30 year fixed

n 15 year fixed

sh nchfa fha 30 year

c 30 year fixed homepath

c 7 yr arm homepath

;

data want(keep=y);

retain prxId;

set have;

if _n_ = 1 then prxId = prxparse("/\d+\s+y(ea)*r/i");

call prxsubstr(prxId, string, pos, len);

if pos>0 then do;

     y = substr(string,pos, len);

     output;

     end;

run;

proc print; run;

PG

PG Slightly improved the pattern

PG
Valued Guide
Posts: 765

Re: extract a substring from a string

hi ... another idea ...

data want;

length y $10;

set have;

y = catx(' ',scan(substr(string,findc(string,,'d')),1),scan(substr(string,findc(string,,'d')),2));

run;

Super Contributor
Posts: 1,636

Re: extract a substring from a string

how about:

data  have;

input string $40.;

datalines;

c 10 year fixed

n 30 year fixed

n 15 year fixed

sh nchfa fha 30 year

c 30 year fixed homepath

c 7 yr arm homepath

;

data want(keep=newSmiley Happy;

  length new_string $ 7;

  set have;

  new_string=catx(' ',compress(string,,'kd'),'year');

  proc print;run;

Linlin

Respected Advisor
Posts: 4,641

Re: extract a substring from a string

Linlin, seems like QLi wants 'yr' on the last line. Smiley Happy

PG

PG
Super Contributor
Posts: 1,636

Re: extract a substring from a string

PG, 

In that case, my code will not work.Smiley Sad

Solution
‎06-13-2012 10:21 PM
Super User
Posts: 9,662

Re: extract a substring from a string

Another PRX Function.

data  have;
input string $40.;
datalines;
c 10 year fixed
n 30 year fixed
n 15 year fixed
sh nchfa fha 30 year
c 30 year fixed homepath
c 7 yr arm homepath
;
run;
data want(drop=pid);
 set have;
 length year $ 20;
 retain pid;
 if _n_ eq 1 then pid=prxparse('/(\d+\s*(year|yr))/i');
 if prxmatch(pid,string) then year=prxposn(pid,1,string);
run; 

Ksharp

Respected Advisor
Posts: 4,641

Re: extract a substring from a string

Very nice Ksharp!

PG

PG
Contributor QLi
Contributor
Posts: 57

Re: extract a substring from a string

It works great. Thanks, All

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 528 views
  • 10 likes
  • 5 in conversation