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;
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.
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.