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;
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.
if you have like 50 different words to look for, they are stored in a table of some sort right?
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.
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.
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.
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!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.