BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MABRINCH
Fluorite | Level 6

I would like to make a variable "Diagn"=1 if Code_1 and/or Code_2 and/or Code_3 and/or Code_4 = M870 or M871

dataset with the desired new variable included: 

ID  Matnr Code_1 Code_2 Code_3 Code_4 Proc diagn
19  1     P200   T500   M870          500    1
19  2     P400   M872                 500 
20  1     P400   M872                 700 
20  2     P200   T800   T812   M871   700    1

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

"and/or" doesn't make any logical sense in this case

 

here is the code using OR

 

data want;
    set have;
    diagn=whichc('M870',of code_:)>0 or whichc('M871',of code_:)>0;
run;
--
Paige Miller

View solution in original post

4 REPLIES 4
Shmuel
Garnet | Level 18

Check next code:

data want;
 set have;
      array cd {4} code_1 - code_4;
     diagn = 0;
      do i=1 to dim(cd);
           if cd(i) in ('M870' , 'M871') then diagn=1;
      end;
run;
PaigeMiller
Diamond | Level 26

"and/or" doesn't make any logical sense in this case

 

here is the code using OR

 

data want;
    set have;
    diagn=whichc('M870',of code_:)>0 or whichc('M871',of code_:)>0;
run;
--
Paige Miller
novinosrin
Tourmaline | Level 20

data have;
input (ID  Matnr Code_1 Code_2 Code_3 Code_4) ($) Proc;
cards;
19  1     P200   T500   M870   .       500    
19  2     P400   M872    .     .        500 
20  1     P400   M872    .      .      700 
20  2     P200   T800   T812   M871   700   
;

data want;
 set have;
 array t code_1-code_4;
 diagn="M870" in t or "M871" in t;
run;

proc print noobs;run;

ID Matnr Code_1 Code_2 Code_3 Code_4 Proc diagn
19 1 P200 T500 M870   500 1
19 2 P400 M872     500 0
20 1 P400 M872     700 0
20 2 P200 T800 T812 M871 700 1

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1304 views
  • 5 likes
  • 4 in conversation