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;
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;
@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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.