Can someone help me with a SAS code where a variable, MET, indicates if one of the variables CODE_1-CODE_44 contains one of several codes (T1b, T4a, T3a) and that the subsequent variable (of CODE_1-CODE_44) contains one of several codes (M1, M2, U3).
CODE_1 CODE_2 CODE_3 CODE_4 ..... MET
T1b M1 C2a P4 1
T4a M4 C2a T4a .
T4a M4 T1b M1 1
UNTESTED CODE
data want;
set have;
array c code_1-code_44;
met=.;
do i=1 to dim(c)-1;
if c(i) in ('T1b','T4a','T3a') and c(i+1) in ('M1','M2','U3') then do;
met=1;
leave;
end;
end;
drop i;
run;
This too is untested, in the absence of sample data in the form of a working data step:
data want (drop=i _:);
array search_strings {9} $8 _temporary_;
length _part1 $3 _part2 $2 _code_string $200;
if _n_=1 then do _part1="T1b","T4a","T3a";
do _part2="M1","M2","U3";
i+1;
search_strings{i}=catx('!',_part1,_part2);
put i= search_strings{i}=;
end;
end;
set have;
_code_string=catx('!',of code_:);
met=0;
do i=1 to dim(search_strings) until (met=1);
if findw(_code_string,trim(search_strings{i}),'!')>0 then met=1;
end;
run;
This works by forming an array of compound terms to search for - each compound term (for example "T1b!M1") formed by concatenating one of the _PART1 expressions followed by a "!" followed by one of the _PART2 expressions - nine compound expressions in your case. The array is declared as _TEMPORARY_ meaning it will not be output, but will be retained throughout the data step.
Then do a similar concatenation of the 44 CODE_ values, also with '!' as a separator. Search that concatenated string for each of the 9 compound search terms until there is success, or you've exhausted the search terms.
Make sure you declare sufficient LENGTHs for the array elements and the other needed variables.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.