BookmarkSubscribeRSS Feed
MABRINCH
Fluorite | Level 6

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

2 REPLIES 2
PaigeMiller
Diamond | Level 26

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;
        
--
Paige Miller
mkeintz
PROC Star

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.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 2 replies
  • 658 views
  • 1 like
  • 3 in conversation