BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cmoore
Obsidian | Level 7

Hi,

 

I have run the code (please see below)

 

data have;
input = "CRH PLC TO BID FOR PPC LTD";
run;

data want;

set have;

temp=input;
p=prxmatch('/\b(TO BID)\b/i',temp);

do while(p);
temp=substr(temp,p);
temp=substr(temp,findc(temp,' ')+1);
Buy=catx(' ',scan(temp,1,' '),scan(temp,2,' '),scan(temp,3,' '),scan(temp,4,' '));

p=prxmatch('/\b(TO BID)\b/i',temp);
end;

drop temp p;
run;

which uses an anchor word "TO BID" and brings back exactly the data I require after the anchor which is "BID FOR PPC LTD"

 

Do you happen to know how I can reverse this and take words before the anchor word "TO BID" ? I am wanting to obtain the text "CRH PLC TO BID"

 

Thanks


Chris

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Or simply:

data have;
  input="CRH PLC TO BID FOR PPC LTD";
  after=substr(input,index(input,"TO BID")+6);
  before=substr(input,1,index(input,"TO BID")+5);
run;

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Or simply:

data have;
  input="CRH PLC TO BID FOR PPC LTD";
  after=substr(input,index(input,"TO BID")+6);
  before=substr(input,1,index(input,"TO BID")+5);
run;
ChrisNZ
Tourmaline | Level 20

How far back do you want to go?

This may help:

data WANT;
  STR  = "CRH PLC TO BID FOR PPC LTD";
  WANT = prxchange('s/(.*)\bTO BID\b.*/$1/i',1,STR);
  put WANT=;
run;

WANT=CRH PLC

Also look at the prxsubstr() function.

 

cmoore
Obsidian | Level 7

Thanks for your reply. So if I wanted to go back and retrieve "

"CRH PLC TO BID"

 and also

"BID FOR PPC LTD"

 

how would I tweak the code? It's a function that I have never used before. I'll need to do some reading up around the subject.

 

Many thanks

Chris

ChrisNZ
Tourmaline | Level 20

What is wrong with @RW9's solution? You never stated what the extraction criteria are.

If you have never used regular expressions, the learning curve can be steep.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 4 replies
  • 1108 views
  • 0 likes
  • 3 in conversation