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
"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;
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;
"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;
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 |
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.