DOT
DOT

Reputation: 329

Loop for checking multiple conditions in SAS with or without MACROS

I am new to SAS and read SAS if statement in do loop and I am still confused on how to solve the loop problem. I also have no idea to use Macro here.

The code is given belòow:

if DIA_PRINCd=001
or DIA_UNOd  =001
or DIA_DUEd  =001
or DIA_TREd  =001
or DIA_QUATTROd=001
or DIA_CINQUEd =001

or DIA_PRINCd=002
or DIA_UNOd  =002
or DIA_DUEd  =002
or DIA_TREd  =002
or DIA_QUATTROd=002
or DIA_CINQUEd=002
--------------------
---------------------
or DIA_PRINCd=142
or DIA_UNOd=142
or DIA_DUEd=142
or DIA_TREd=142
or DIA_QUATTROd=142
or DIA_CINQUEd=142
;

run;

May I know how to use loop in SAS for this problem?

Upvotes: 1

Views: 620

Answers (1)

Richard
Richard

Reputation: 27526

It looks like the same set of variables are being tested for a variety of diagnostic codes, such as 001, 002, ..., 142 as criteria for a subsetting if.

The or operator means that if a single sub-clause is true the subset should occur.

You can create a variable array for the DIA_* and loop over the codes you want to test. Within the loop the WHICHN function call tell you if any of the variables in the array matches a code. If the diagnoses values are actually character type "001", use WHICHC.

Example:

  array diagnoses dia_:;
  found = 0;
  do code = 001, 002, ..., 142;                 /* list of codes to check */
    found = whichn(code, of diagnoses(*));      /* check if present */  
    if found then leave;                        /* stop checking at earliest find */
  end;

  if found;    /* subsetting if.  At least one of the variables matched one of the codes */

The WHICHN and WHICHC functions implicitly loop over the values passed to it (i.e. the values from the variables in the of array-name(*))

Upvotes: 1

Related Questions