I am trying to write a macro that takes a 2 word input as well as a numeric value (to specify the range of lag) and outputs only the situations where the condition is met that the 2nd word appears at most 20 observations after the first word. The input table looks something like this:
ID | Word |
12 | toby |
13 | hot |
14 | cat |
15 | drop |
So if I call a macro %keywordsearch(phrase = hot drop, lagval = 1), it should return an empty table but if I call the macro with lagval = 2 or greater, it should return a table with a single row containing id = 15. How would I do this?
Is it necessary with a macro?
I need the macro so that I can search for different words and with different ranges. But if you can do it without a macro, I can figure out how to adapt it to a macro.
Here's an example:
/* Create data */
data words ;
input word $ ;
cards ;
toby
hot
cat
drop
;
data find ;
retain
/* counter and flag */
cnt 0
foundWord1 0
/* parameters */
word1 "hot"
word2 "drop"
lag 1 ;
set words;
/* add 1 to counter if first word has been found */
if foundWord1=1 then do ;
cnt=cnt+1 ;
end ;
/* set flag if you find word 1 */
if word=word1 then do ;
foundWord1=1 ;
end ;
/* for debugging */
put word= cnt= word1= word2= lag= ;
/* if find word2 and cnt<=lag then success */
if word=word2 and cnt<=lag then do ;
put "FOUND " cnt= word1= word2= ;
end ;
run ;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.