I would like to use the SAS array statement to create 3 new variables(yes or no) from 48 variables that contain just 3 drug classes, Benzos, Opioids and Anticholinergics. So the 48 variables are listed as drugflag1-drugflag48.
The new variables 1-3 are Benzo_rc, Opioid_rc and Antichol. For variable Benzo, if drugflag1- drugflag48 has Benzo then the Variable Benzo_rc="Yes" if not Benzo_rc
Data Merged_data3;
Set Merged_data3;
array drugname $DrugFlag1-DrugFlag48;
do i = 1 to 48;
IF drugname[i]= BENZO THEN benzo_rc = 1; ELSE benzo_rc = 0;End;Run;
="No".
I have tried the following code but it is not working. Please, I need help.
Please, could you send a link? I could not find it online.
Thank you.
Google: WHICHC SAS function site:documentation.sas.com
Your "else" branch causes you to only pick up the value for the 48th variable.
Such loops need to be coded differently:
data Merged_data4; /* don't overwrite your input */
set Merged_data3;
array drugname $DrugFlag1-DrugFlag48;
benzo_rc = 0;
do i = 1 to 48;
if drugname[i] = "BENZO"
then do;
benzo_rc = 1;
leave;
end;
end;
run;
The LEAVE statement will end the loop quickly if a match is found.
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.