BookmarkSubscribeRSS Feed
Ani7
Obsidian | Level 7

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:

IDWord
12toby
13hot
14cat
15drop

 

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?

3 REPLIES 3
Ani7
Obsidian | Level 7

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.

AMSAS
SAS Super FREQ

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 ;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 644 views
  • 0 likes
  • 3 in conversation