Note this assumes only one occurence of the word, otherwise you will get the last one:
data want;
length before after $200;
string="word word word word word WORD_BEFORE trigger WORD_AFTER word word word word";
do i=1 to countw(string," ");
if scan(string,i," ")="trigger" then do;
before=scan(string,i-1," ");
after=scan(string,i+1," ");
end;
end;
run;
Note this assumes only one occurence of the word, otherwise you will get the last one:
data want;
length before after $200;
string="word word word word word WORD_BEFORE trigger WORD_AFTER word word word word";
do i=1 to countw(string," ");
if scan(string,i," ")="trigger" then do;
before=scan(string,i-1," ");
after=scan(string,i+1," ");
end;
end;
run;
Hi,
This is great! Thank you.
I am wondering how you would do this iteratively. I am trying to parse the word before a trigger word, but sometimes the trigger word appears more than once. I need to be able to pull it each time the trigger word appears. The word before is different every time, but the trigger is the same.
something like:
string="word word word word word WORD_BEFORE_1 trigger WORD_AFTER word word word word WORD_BEFORE_2 trigger WORD_AFTER WORD_BEFORE_3 trigger WORD_AFTER";
WORD_BEFORE_1=
WORD_BEFORE_2=
WORD_BEFORE_3=
etc.
It is a good idea to start a new topic, provided test data and required output. This topic is closed, and over a year old. I would suggest a simple loop, i=1 to scan(your_string), and check each word within the loop.
Thanks, I posted it as a new topic here:
Here is one way:
data have; informat string $80.; input string &; cards; now is the time this must stop Now hear this and do it quickly ; data want; set have; WORD_BEFORE = scan(string,findw(string,"this",' ',"e")-1," "); WORD_AFTER = scan(string,findw(string,"this",' ',"e")+1," "); run;
Art, CEO, AnalystFinder.com
If the trigger value is not present then this code would generate word_before=last-word-of-string, and word_after=first-word-of-string, with no attendent NOTEs on the log.
And if the trigger appears as the first word, then word_before generates a note, Or if the word appears only as the last word, then word_after generates a note.
If these condition are to be avoided, I'd recommend a minor alteration of @art297's response:
data want (drop=ix);
set have;
ix=findw(string,"stop",' ','e');
if ix>1 then WORD_BEFORE = scan(string,ix-1," ");
if ix<countw(string) and ix^=0 then WORD_AFTER = scan(string,ix+1," ");
run;
Another way
data have;
string="word word word word word WORD_BEFORE trigger WORD_AFTER word word word word";
word_before= prxchange('s/(.+)(WORD_BEFORE)(.+)/$2/', -1, string);
word_after= prxchange('s/(.+)(WORD_AFTER)(.+)/$2/', -1, string);
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.