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

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Save $200 when you sign up by March 14!

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