Wednesday, October 19, 2016

SAS - Example of COBOL-like structure

An example of COBOL-like structure in a SAS program.



/*
* BQ 20161018
* Match rule violations to reportable injuries.
* Append Rptl_Dmy to output rule violation.
* Logic assumes both input files sorted by EmployeeID and Date.
* I am doing this because there are so many rules, I need to
* thin the herd before attempting any real association analysis.
*/

%let MAX_DAYS_VIOLATION_TO_INJURY = 30;

data
      &TBL_ALL_RULES_MATCHED
      &TBL_INJURIES_WITHOUT_VIOLATION
  ;

retain match_found;

* Begin what I always called "mainline" in COBOL. ;

link READ_RULE;
link READ_INJURY;
do while (eof_rule = 0 AND eof_injury = 0);
      link PROCESS_RTN;
end;
link WRAPUP;
return; * end "mainline";

READ_RULE:
      set &TBL_ALL_RULES
           (where=(date >= &GLBL_MIN_INJURY_DATE)) end=eof_rule;
      return; * end READ_RULE ;

READ_INJURY:
      set &TBL_REPORTABLE_INJURIES_ONLY
           (rename=(EmployeeID = injEmployeeID Date = injDate)) end=eof_injury;
      match_found = 0;
      return; * end READ_INJURY;

PROCESS_RTN:
      if (EmployeeID < injEmployeeID) then link RULE_LOW;
      else if (EmployeeID > injEmployeeID) then link INJURY_LOW;
      else if (Date < (injDate - &MAX_DAYS_VIOLATION_TO_INJURY)) then link RULE_LOW;
      else if (Date >= (injDate - &MAX_DAYS_VIOLATION_TO_INJURY) AND
                 Date <= (injDate + &MAX_DAYS_VIOLATION_TO_INJURY)) then link MATCH;
      else link INJURY_LOW;
      return; * end PROCESS_RTN;

RULE_LOW:
      reportable_injury = 0;
      lead_time_in_days = .;
      output &TBL_ALL_RULES_MATCHED;
      link READ_RULE;
      return; * end RULE_LOW;

INJURY_LOW:
      if (match_found = 0) then do;
           output &TBL_INJURIES_WITHOUT_VIOLATION;
      end;
      link READ_INJURY;
      return; * end INJURY_LOW;

MATCH:
      match_found = 1;
      reportable_injury = 1;
      lead_time_in_days = injDate - Date;
      output &TBL_ALL_RULES_MATCHED;
      * what if violation on Jan 1, violation on Jan 2, injury on Jan 3 ? ;
      * that is why I am only going to read next Rule record (not Injury). ;
      link READ_RULE;
      return; * end MATCH;

WRAPUP:
      do while (eof_rule = 0);
           link RULE_LOW;
      end;
      do while (eof_injury = 0);
           link INJURY_LOW;
      end;
      return;

run;






No comments:

Post a Comment