## Macro for searching multiple variables?

Solved
Frequent Contributor
Posts: 82

# 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
Posts: 3,055

## 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;

--
Paige Miller

All Replies
Solution
‎04-23-2014 01:35 PM
Posts: 3,055

## 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;

--
Paige Miller
Frequent Contributor
Posts: 82

## Re: Macro for searching multiple variables?

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;

Posts: 3,852

## 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.