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;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 849 views
  • 2 likes
  • 3 in conversation