/**********************************************************************/


libname dual '
              /* your-saslib */
                               ' disp=shr;
%let date='06JAN93'd;
%let userid=BOYLE;
%let dd=dual;
%let master=fitness;
%let journal=journal;

proc sort data=&dd..&master;
   by patient;
run;


/**********************************************************************/


proc sort data=&dd..&journal out=journal;
   by key descending datetime;
run;


/**********************************************************************/


   /* Build transaction data set from journal              */
data trans;
set journal(rename=(key=patient));
by patient
   descending datetime;
keep patient age weight runtime rstpulse runpulse
     maxpulse oxygen group datetime code1;
retain found 0
       age weight runtime rstpulse runpulse 
       maxpulse oxygen group;

missing _;

if userid = "&userid" and
   datepart(datetime)="&date"d
   and substr(code,1,1)='2'
then do;
   found=1;
   code1=substr(code,1,1);
      if code1='2' then code1='C';


/**********************************************************************/


      if oldvalun=. then oldvalun='_';
      if code1 ne 'A' then select (varchng);
         when('AGE') age=oldvalun;
         when('WEIGHT') weight=oldvalun;
         when('RUNTIME') runtime=oldvalun;
         when('RSTPULSE') rstpulse=oldvalun;
         when('RUNPULSE') runpulse=oldvalun;
         when('MAXPULSE') maxpulse=oldvalun;
         when('OXYGEN') oxygen=oldvalun;
         when('GROUP') group=oldvalun;
         otherwise;
      end;              /* Select statement                */
   end;                 /* End do                          */
   if last.patient and found=1 then do;
      output;
      age=.;
      weight=.;
      runtime=.;
      rstpulse=.;
      runpulse=.;
      maxpulse=.;
      oxygen=.;
      group=.;
      found=0;
   end;
run;


/**********************************************************************/


data &dd..&master(keep=patient age weight
       runtime rstpulse runpulse maxpulse
       oxygen group)
except;
update &dd..&master(in=inf) trans(in=int);
by patient;
select(code1);
      when('A') return;
      when('C') do;
            if not inf then output except;
            output &dd..&master;
         end;
      when('D') do;
               if inf then output except;
               output &dd..&master;
         end;
       otherwise output &dd..&master;
   end;        /* End select                               */
run;


/**********************************************************************/


proc sort data=&dd..&journal out=journal;
by key datetime;
run;

   /* Build transaction data set from journal data set     */
data trans;
   set journal(rename=(key=patient));
   by patient datetime;
   keep patient age weight runtime rstpulse runpulse 
        maxpulse oxygen group datetime code1;
   retain age weight runtime rstpulse runpulse
          maxpulse oxygen group;
   missing _;
   if datepart(datetime)>="&date"d
   then do;

         /* Build transaction obs                          */
      code1=substr(code,1,1);
      if code1='2' then code1='C';
      if newvalun=. then newvalun='_';
      select(varchng);
         when('AGE') age=newvalun;
         when('WEIGHT') weight=newvalun;
         when('RUNTIME') runtime=newvalun;
         when('RSTPULSE') rstpulse=newvalun;
         when('RUNPULSE') runpulse=newvalun;
         when('MAXPULSE') maxpulse=newvalun;
         when('OXYGEN') oxygen=newvalun;
         when('GROUP') group=newvalun;
         otherwise;
     end;        /* Select statement                       */
     if last.patient then do;
        output;
        age=.;
        weight=.;
        runtime=.;
        rstpulse=.;
        runpulse=.;
        maxpulse=.;
        oxygen=.;
        group=.;
     end;
   end;        /* End if datepart... then do               */
run;

   /* Update master data set with transactions             */
data &dd..&master(keep=patient age weight
          runtime rstpulse runpulse maxpulse
          oxygen group);
   update &dd..&master trans;
   by patient;
   if code1='D' then return;
   else output;
run;


/**********************************************************************/


data temp;
set &dd..&journal;
if date(datetime) = today() - 1;
c1=substr(code,1,1);
run;
proc sort data=temp;
by datetime key c1;
run;


/**********************************************************************/


data _null_;
retain addcnt delcnt chngcnt dualcnt page 0 date;
length activity $9;
set temp end=eof;
by datetime key c1;
c2=substr(code,2,1);
file print header=hdr ps=60 n=1;
if _n_ = 1 then date=today();
if first.c1 then do;
select(c1);
   when('A') do;
      activity='ADD';
      addcnt+1;
   end;
   when('D') do;
      activity='DELETED';
      delcnt+1;
   end;
   when('C') do;
      activity='UPDATE';
      chngcnt+1;
   end;
   when('2') do;
      activity='DUAL UPDT';
      dualcnt+1;
      chngcnt+1;
   end;
   otherwise do;
      put / 'Invalid code encountered. ' code= /;
   end;
   end;        /* Select clause                            */
end;
if first.datetime then put @1 userid @9 datetime @;
if first.key then put @29 key @;
if first.c1 then put @40 activity @;
put @51 varchng @;
if c1 in ('C','2','D') then do;
   if c2= 'N' then
      put   @66 'New: ' newvalun @;
   else put @66 'New: ' newvaluc @;
   if c1 = 'C' or c1='2'  then do;
      if c2='N' then do;       /* Numeric change           */
         if oldvalun = . then put;
         else put / @66 'Old: ' oldvalun;
      end;
      else do;        /* Character change                  */
         if oldvaluc = ' ' then put;
         else put / @66 'Old: ' oldvaluc;
      end;
   end;
end;
else do;
   if c2='N' then put @66 newvalun;
   else put @66  newvaluc;
end;
if eof then do;
   file print notitles;
   put /// 'FSEDIT TRANSACTION SUMMARY';
   put / 'Total Records Added:     ' addcnt
       / 'Total Records Deleted:   ' delcnt
       / 'Total Records Updated:   ' chngcnt
      // 'Total Records Updated via Dual Data Entry:
        ' dualcnt //;
   end;
return;
hdr: page+1;
     put 'Journal Transaction Log for ' date date7.
     @125 'Page ' page
     //  @1  'Userid'
     @9  'DateTime'
     @29 'Key Value'
     @40 'Code'
     @51 'Variable'
     /  @1  '___' 
     @9  '_________' 
     @29 '_____' 
     @40 '__' 
     @51 '____';
return;
run;


/**********************************************************************/