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.

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

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
  • 1256 views
  • 2 likes
  • 3 in conversation