DATA Step, Macro, Functions and more

Subset Data Based on Drug Name Using the Find Function

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Subset Data Based on Drug Name Using the Find Function

[ Edited ]

Hello!

 

I am relatively new to SAS.

 

Task: I need to subset my data based on "drug name," since I am studying a particular class. To accomplish this, I need to search the names and export all observations to a new data set. 

 

I am able to use the find function to subset the data using the following code:

 

data new;
set input;
if find(drug,'Fluoxetine','i') ge 1;
run;
proc print data=new
run;

 

data new1;
set input;
if find(drug,'Sertraline','i') ge 1;
run;
proc print data=new1;
run;

 

However, I would need to do this for each drug name and then merge the data sets based on subject ID. 

 

What method would you suggest to efficiently accomplish this? 

 

Thanks for your help!


Accepted Solutions
Solution
‎01-23-2018 09:11 PM
Super User
Posts: 22,844

Re: Subset Data Based on Drug Name Using the Find Function

Rather than multiple data pulls, do it all in one pull using a temporary array. Assuming you have 5 drugs you're searching for something like the following may work (untested, I suspect my array definition is incorrect, you may need a _character_ to specify it's a character array).

 

 

data want;

set input;

array _drugs(5) _temporary_ ('Fluoxetine', 'Sertaline', 'random1', 'random2', 'random3');

flag=0;
do i=1 to dim(_drugs);

if find(drug, _drugs(i), 'i') > 0 then do;
flag=1;
leave;
end;

end;

if flag=1 then output; *keeps only records of interest; run;

View solution in original post


All Replies
Solution
‎01-23-2018 09:11 PM
Super User
Posts: 22,844

Re: Subset Data Based on Drug Name Using the Find Function

Rather than multiple data pulls, do it all in one pull using a temporary array. Assuming you have 5 drugs you're searching for something like the following may work (untested, I suspect my array definition is incorrect, you may need a _character_ to specify it's a character array).

 

 

data want;

set input;

array _drugs(5) _temporary_ ('Fluoxetine', 'Sertaline', 'random1', 'random2', 'random3');

flag=0;
do i=1 to dim(_drugs);

if find(drug, _drugs(i), 'i') > 0 then do;
flag=1;
leave;
end;

end;

if flag=1 then output; *keeps only records of interest; run;
Occasional Contributor
Posts: 6

Re: Subset Data Based on Drug Name Using the Find Function

Thank you! This worked with few adjustments.
PROC Star
Posts: 2,225

Re: Subset Data Based on Drug Name Using the Find Function

Do you need new datasets?

How about this?


 proc print data=input; 
  where find(drug,'Sertraline','i') ; 
run;

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 100 views
  • 2 likes
  • 3 in conversation