BookmarkSubscribeRSS Feed
UcheOkoro
Lapis Lazuli | Level 10

I am trying to create a variable using an array statement. I have a dataset with variables Drug1 - Drug48. The drugs include opioid, benzo, anticholinergic and antibiotics. I want to create 3 new variables that are Opioid, benzo and anticholinergic will be " yes" or "no" . I need help. I tried using this code below but it did not help.

 

DATA Merged_data3;
Set Merged_data2;
array Drug_list [*] DrugFlag1-DrugFlag48;
All_Drugs= catx(' ', of Drug_list[*]);
run;

2 REPLIES 2
DavePrinsloo
Pyrite | Level 9

 

Try something like this:

DATA Merged_data3;
Set Merged_data2;
array Drug_list [*] DrugFlag1-DrugFlag48;
opoid_flg = 0; benzo_flg=0;
do over drug_list;
   if index(upcase(drug_lst),'OPIOD') then opoid_flg =1; 
   if index(upcase(drug_lst),'BENZO') then benzo_flg =1; 
end;
run;
Jagadishkatam
Amethyst | Level 16

@UcheOkoro you question is not clear, you said you want to create 3 new variables but its not clear. However if you want to concatenate all the drugs in drug1-drug48 variables then you can do as below with arrays

 

data want;
set merged_data2;
array drug_list(*) $ drugflag1-drugflag48;
array drug_name(*) $ drugnam1-drugnam48;
do i = 1 to dim(drug_list);
if drug_list(i)='Y' then drug_name(i)=vname(drug_name(i));
if drug_name(i)='opioid' the opioid='Yes';
if drug_name(i)='benzo' the benzo='Yes';
if drug_name(i)='anticholinergic' the anticholinergic='Yes';
end;
all_drugs=catx(' ', of Drug_nam[*]); run;
Thanks,
Jag

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 2 replies
  • 990 views
  • 0 likes
  • 3 in conversation