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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

3 REPLIES 3
Reeza
Super User

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;
mglogan
Obsidian | Level 7
Thank you! This worked with few adjustments.
ChrisNZ
Tourmaline | Level 20

Do you need new datasets?

How about this?


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

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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