BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
UcheOkoro
Lapis Lazuli | Level 10

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Look up WHICHC() instead.

Benzo_RC = whichc('BENZO', of drugname(*))>1;
Opioid_RC = whichc('OPIOID', of drugname(*))>1;

View solution in original post

4 REPLIES 4
Reeza
Super User
Look up WHICHC() instead.

Benzo_RC = whichc('BENZO', of drugname(*))>1;
Opioid_RC = whichc('OPIOID', of drugname(*))>1;
UcheOkoro
Lapis Lazuli | Level 10

Please, could you send a link? I could not find it online.

 

Thank you.

Reeza
Super User

Google: WHICHC SAS function site:documentation.sas.com

Kurt_Bremser
Super User

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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