DATA MASTER (INDEX = (VARIABLE=(VARIABLE) ) );
INPUT
@03 VARIABLE $CHAR2.
@10 D_COUNT 1.
@15 DEP_01 $CHAR5.
@20 DEP_02 $CHAR5.
@25 DEP_03 $CHAR5.
@30 DEP_04 $CHAR5.
@35 DEP_05 $CHAR5.
@40 DEP_06 $CHAR5.
@45 DEP_07 $CHAR5.
@50 DEP_08 $CHAR5.
@55 DEP_09 $CHAR5.
@60 DEP_10 $CHAR5.
;
CARDS;
  AA     6    BB   CC   DD   EE   FF   GG
  BB     2    GG   HH
  CC     3    EE   FF   HH
  DD     3    FF   GG   HH
  EE     0
  FF     1    MM
  GG     0
  HH     1    II
  II     1    JJ
  JJ     1    KK
  KK     1    LL
  LL     1    MM
  MM     0
;;;;

 /*******************************************************************
 /* MACRO DEPEND - A PSEUDO-RECURSIVE MACRO
 /******************************************************************/
 %MACRO DEPEND(UP_LVL,IDX_VAR,NEXT_LVL);

 LEVEL = LEVEL + &UP_LVL;
 LINK PUSH_VAR;

 DO &IDX_VAR = 1 TO 10;
 IF T_STACK{LEVEL,&IDX_VAR} NOT = ' '
 THEN DO;
 VARIABLE = T_STACK{LEVEL,&IDX_VAR};
 LINK GET_MSTR;

 IF (READ_OK = 'OK') THEN OUTPUT;

 IF (T_COUNT{LEVEL} > 0 AND READ_OK = 'OK')
 THEN DO;
 T_COUNT{LEVEL} = T_COUNT{LEVEL} - 1;
 LINK DEPEND&NEXT_LVL;
 END;
 END;
 END;

 LINK POP_VARS;
 LEVEL = LEVEL - &UP_LVL;
 %MEND DEPEND;
 /*******************************************************************
 /* SEARCH - A ROUTINE TO FIND ALL DEPENDENT VARIABLES, GIVEN A LIST
 /******************************************************************/

 DATA DEP_LIST(KEEP=VARIABLE LEVEL ) /STACK=40;

 ARRAY DEP {10} $5 DEP_01-DEP_10;

 ARRAY VAR_NAME {0:10} $5 _TEMPORARY_;
 ARRAY T_COUNT {0:10} 5 _TEMPORARY_;
 ARRAY T_STACK {0:10,10} $5 _TEMPORARY_;

 RETAIN LEVEL 0;

 INFILE CARDS; /* VARLIST is a user-supplied filename reference */
 INPUT @01 VARIABLE $CHAR5.; /* adjust ** to data location on line */

 LEVEL = 0;

 LINK GET_MSTR;

 IF READ_OK = 'OK' THEN DO;
 OUTPUT;
 IF D_COUNT > 0 THEN LINK DEPEND;
 END;

 RETURN;
 /*******************************************************************
 /* CLEAR VARIABLE STACK.
 /******************************************************************/
 EMPTY_ST:

 DO Y = 0 TO 10;

 VAR_NAME{Y} = '';
 T_COUNT {Y} = 0;

 DO Z = 1 TO 10;
 T_STACK {Y,Z} = '';
 END;
 END;

 RETURN;
 /*******************************************************************
 /* POP VARIABLE AND DEPENDENCIES FROM A STACK.
 /******************************************************************/
 POP_VARS:

 VARIABLE = VAR_NAME{LEVEL};
 D_COUNT = T_COUNT {LEVEL};

 VAR_NAME{LEVEL} = '';
 T_COUNT {LEVEL} = 0;

 DO Z = 1 TO 10;
 DEP{Z} = T_STACK {LEVEL,Z};
 T_STACK {LEVEL,Z} = '';
 END;

 RETURN;
 /*******************************************************************
 /* PUSH VARIABLE AND DEPENDENCIES ONTO A STACK.
 /******************************************************************/
 PUSH_VAR:

 VAR_NAME{LEVEL} = VARIABLE;
 T_COUNT {LEVEL} = D_COUNT;

 DO Z = 1 TO 10;
 T_STACK {LEVEL,Z} = DEP {Z};
 END;

 RETURN;
 /******************************************************************
 /* SELECT OUT OF GROUP DEPENDENCIES.
 /*****************************************************************/
 DEPEND : LINK EMPTY_ST ;
 %DEPEND(0,A,0); RETURN;
 DEPEND0: %DEPEND(1,B,1); RETURN;
 DEPEND1: %DEPEND(1,C,2); RETURN;
 DEPEND2: %DEPEND(1,D,3); RETURN;
 DEPEND3: %DEPEND(1,E,4); RETURN;
 DEPEND4: %DEPEND(1,F,5); RETURN;
 DEPEND5: %DEPEND(1,G,6); RETURN;
 DEPEND6: %DEPEND(1,H,7); RETURN;
 DEPEND7: %DEPEND(1,I,8); RETURN;
 DEPEND8: %DEPEND(1,J,9); RETURN;
 DEPEND9: %DEPEND(1,K,A); RETURN;
 DEPENDA: ABORT RETURN 1111 ; RETURN;
 /******************************************************************
 /* LOOK UP VARIABLE NAME ON THE MASTER LIST OF VARIABLES.
 /*****************************************************************/
 GET_MSTR:
 _IORC_ = 0;
 _ERROR_ = 0;

 SET MASTER(KEEP=VARIABLE D_COUNT DEP_01-DEP_10
     ) KEY=VARIABLE/UNIQUE;

 IORC_ = RESOLVE(_IORC_);
 READ_OK = 'NO';
 SELECT (IORC_);
 WHEN(%SYSRC(_DSEMTR)) PUT 'MULT TRANS NOT ON MASTER ';
 WHEN(%SYSRC(_DSENMR)) PUT 'TRANS NOT ON MASTER ';
 WHEN(%SYSRC(_DSENOM)) PUT 'NOT ON MASTER ';
 WHEN(%SYSRC(_SEDSKFL)) PUT 'TAPE/DISK IS FULL ';
 WHEN(%SYSRC(_SEINVPS)) PUT 'VALUE OF POSITION INVALID ';
 WHEN(%SYSRC(_SENOCHN)) PUT 'NO CHANGE/NO DUPS ALLOWED ';
 WHEN(%SYSRC(_SENOLCK)) PUT 'RECORD LOCK NOT AVAILABLE ';
 WHEN(%SYSRC(_SENODEL)) PUT 'RECORD MAY NOT BE DELETED ';
 WHEN(%SYSRC(_SERECRD)) PUT 'NO REC READ FROM INPUT ';
 WHEN(%SYSRC(_SWDLREC)) PUT 'REC DELETED FROM FILE ';
 WHEN(%SYSRC(_SWNOUPD)) PUT 'RECORD CANNOT BE UPDATED ';
 WHEN(%SYSRC(_SWNOWHR)) PUT 'NO LONGER SATISFY "WHERE" ';
 OTHERWISE READ_OK = 'OK';
 END;

 _ERROR_=0;

 RETURN;
 CARDS;
BB
EE
FF
;;;;

PROC PRINT ;
RUN;