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!
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;
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;
Do you need new datasets?
How about this?
proc print data=input;
where find(drug,'Sertraline','i') ;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.