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

I am looking to create a flag variable ="Y" in my example  if my "aetype" variable contains  any of the words (Nausea or headache) in it. I am little aware of using "FIND" or "INDEX" or "FINDW". So I have to write condition for every word? because I have alike 50 different words if those are present in "aetype" then  I need flag="Y". Is there any  efficient way? Thank you for your suggestions.

Data adverse;
	Input Sid visit$ aetype$ severity$;
	datalines;
101 v1 rashes mild
102 v2 vomitings moderate
103 v3 headache severe
;
run;

data adverse1;
length aetype $30.;
set adverse;
if aetype= "rashes" then aetype= 'rashes,Nausea';
run;

SASuserlot_0-1645645643957.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

One way is a temporary array and then you search with those.

data adverse1;
   length aetype $30.;
   set adverse;
   array words (2) $ 15 _temporary_  ('Nausea', 'headache');
   do i= 1 to dim(words);
      if findw(aetype,strip(words[i]),' ','i')>0 then do;
         flag='Y';
         leave;
      end;
   end;
   drop i;
run;

temporary arrays do not have the values written to the output data set. You specify the number of elements, 2 in this case and for character values you use $ and a number to set the maximum number of characters to store, the keyword _temporary_ and then a list of the values in quotes for character as usual.

The Dim function returns the number of elements defined in the array so you can search for each word in order from left to right as defined in the array statement.

You say you are familiar with Find and Findw so no details though you may not realize that you may need to strip (remove) leading and trailing blanks for the comparison. The LEAVE statement tells the loop to stop when encountered. So if you want to stop when the first word in the list is found then issue a leave.

 

This used findw so "headaches" would not match "headache" in the search list. Similar behavior could be done with FIND or INDEX or INDEXW per the syntax.

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

if you have like 50 different words to look for, they are stored in a table of some sort right?

SASuserlot
Barite | Level 11

I have the condition provided like 50 words ( which I have the list). they are expecting those words will be in aetype variable as single word or combination with others. FYI: I created the adverse1 dataset, so that I can have the word in combination ( Nausea). My interest words are Nausea and headache.

ballardw
Super User

One way is a temporary array and then you search with those.

data adverse1;
   length aetype $30.;
   set adverse;
   array words (2) $ 15 _temporary_  ('Nausea', 'headache');
   do i= 1 to dim(words);
      if findw(aetype,strip(words[i]),' ','i')>0 then do;
         flag='Y';
         leave;
      end;
   end;
   drop i;
run;

temporary arrays do not have the values written to the output data set. You specify the number of elements, 2 in this case and for character values you use $ and a number to set the maximum number of characters to store, the keyword _temporary_ and then a list of the values in quotes for character as usual.

The Dim function returns the number of elements defined in the array so you can search for each word in order from left to right as defined in the array statement.

You say you are familiar with Find and Findw so no details though you may not realize that you may need to strip (remove) leading and trailing blanks for the comparison. The LEAVE statement tells the loop to stop when encountered. So if you want to stop when the first word in the list is found then issue a leave.

 

This used findw so "headaches" would not match "headache" in the search list. Similar behavior could be done with FIND or INDEX or INDEXW per the syntax.

SASuserlot
Barite | Level 11

Thanks @ballardw . I will try this as soon as I can. I will let you know if I encounter any issues. thanks for explaining about the problems with other functions.

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