DATA Step, Macro, Functions and more

Macro for searching multiple variables?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 82
Accepted Solution

Macro for searching multiple variables?

Hello - I'm analyzing a healthcare claims database with one row per discharge.  Each row has 41 variables representing 41 discharge diagnosis codes (variables dx_code1-dx_code41).  I am trying to identify all discharges with one of four diagnosis codes.  I could manually type this out but I'm sure there must be a more elegant way (macro?) to do this.  Any help would be appreciated.  Thanks!

data discharges;

where

dx_code1 in: ('0199', '7806', '78034', '7213') or

dx_code2 in: ('0199', '7806', '78034', '7213') or

...

dx_code41 in: ('0199', '7806', '78034', '7213');

run;


Accepted Solutions
Solution
‎04-23-2014 01:35 PM
Trusted Advisor
Posts: 1,931

Re: Macro for searching multiple variables?

Maybe an ARRAY would work

Something like

/* UNTESTED CODE */

data discharges;

     set whatever;

     array dx dx_code1-dx_code41;

     flag=0;

     do i=1 to dim(dx);

          if dx(i) in ('0199', '7806', '78034', '7213') then flag=flag+1;

      end;

     if flag>0 then output;

run;

View solution in original post


All Replies
Solution
‎04-23-2014 01:35 PM
Trusted Advisor
Posts: 1,931

Re: Macro for searching multiple variables?

Maybe an ARRAY would work

Something like

/* UNTESTED CODE */

data discharges;

     set whatever;

     array dx dx_code1-dx_code41;

     flag=0;

     do i=1 to dim(dx);

          if dx(i) in ('0199', '7806', '78034', '7213') then flag=flag+1;

      end;

     if flag>0 then output;

run;

Frequent Contributor
Posts: 82

Re: Macro for searching multiple variables?

Posted in reply to PaigeMiller

Thanks - just needed to be a colon after "in."  Also, I don't think you need the flag = flag + 1 per se; I was able to get it to work just by doing:

if dx(i) in: ('0199', '7806', '78034', '7213') then flag=1;

I appreciate your help.

Respected Advisor
Posts: 3,799

Re: Macro for searching multiple variables?

WHICHC function might be acceptable.

array dx

  • dx_code:;
  • f1=whichC('0199',of DX

  • );
  • f2=whichC('7806',of DX

  • );
  • ...

    flag = max(f1,f2,f3,f4);

    🔒 This topic is solved and locked.

    Need further help from the community? Please ask a new question.

    Discussion stats
    • 3 replies
    • 182 views
    • 0 likes
    • 3 in conversation