SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
QLi
Fluorite | Level 6 QLi
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

8 REPLIES 8
PGStats
Opal | Level 21

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
MikeZdeb
Rhodochrosite | Level 12

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;

Linlin
Lapis Lazuli | Level 10

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=new:);

  length new_string $ 7;

  set have;

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

  proc print;run;

Linlin

PGStats
Opal | Level 21

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

PG

PG
Linlin
Lapis Lazuli | Level 10

PG, 

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

Ksharp
Super User

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

PGStats
Opal | Level 21

Very nice Ksharp!

PG

PG
QLi
Fluorite | Level 6 QLi
Fluorite | Level 6

It works great. Thanks, All

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 8 replies
  • 3083 views
  • 10 likes
  • 5 in conversation