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


%let username=&sysjobid;             /* on MVS system        */
%let journal=fileref.journal;                           
%let master=fileref.master;                             
%let key1=ID;                 /* name of key variable        */


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


FSEINIT: link JRNFINIT;

   /* any other application specific code                    */
                                                 
return;                                           

INIT: link JRN_INIT;                              

   /* any other application specific code                    */
                                                 
return;                                           

MAIN: link JRN_MAIN;                              

   /* any other application specific code                    */
                                                  
return;                                           

TERM: link JRN_TERM;                              

   /* any other application specific code                    */ 
                                                  
return;                                           

FSETERM: link JRNFTERM;                           

   /* any other application specific code                    */ 
                                                  
return;                                           

JRNFINIT:                                         

   /* journal program specific code                          */ 
                                                 
return;                                           

JRN_INIT:

   /* journal program specific code                          */
                                                
return;                                          

JRN_MAIN:                                        

   /* journal program specific code                          */
                                                
   link WRITEJRN;                                  
                                                
                                                
return;                                          

JRN_TERM:                                        

   /* journal program specific code                          */

WRITEJRN:                                      
                                                
                                                
return;                                          

JRNFTERM:                                        

   /* journal program specific code                          */
                                                
return;

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


JRNFINIT:                                                   
   /**********************************************************/
   /* The four macro variables, USERNAME, JOURNAL, MASTER,   */
   /* and KEY1 are created external to the FSEDIT session    */
   /**********************************************************/
control always;                                             

   /* get user making changes                                */                            
length lastcode $2 datetime 8;                              
userid=symget('username');                                  

   /* Open data set for update.                              */                            
   /* map SCL vars to data set                               */                            
   /* data vector (DDV)                                      */                            
journal=open(symget('journal'),'u');                        
call set(journal);                                          

   /* create OLD_RECS and CHANGES data sets                  */           
   /* by using MASTER data set as the model.                 */
   /* Open both temporary data sets for update.              */
call new('old_recs',symget('master'),1,'n');     
dsid_old=open('old_recs','u');                   
call set(dsid_old);                              

call new('changes','old_recs',1,'n');            
dsid_chg=open('changes','u');                    
call set(dsid_chg);                              

   /*_nvar= # of vars in data set                            */              
_nvar = attrn(dsid_old,'nvars');                 

   /* read first records                                     */                   
rc=fetchobs(dsid_old,1);                         
rc=fetchobs(dsid_chg,1);                         

   /* initialize vars for putvar                             */              
num_chng = varnum(journal,'varchng');            
num_oldc = varnum(journal,'oldvaluc');           
num_oldn = varnum(journal,'oldvalun');           
num_newc = varnum(journal,'newvaluc');           
num_newn = varnum(journal,'newvalun');           
num_key1 = varnum(journal,'key1');               
num_code = varnum(journal,'code');               
numokey1 = varnum(dsid_old,symget("key1"));      
return;                                          


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


JRN_INIT:                                  
   /* update OLD_RECS to null record                         */    
if lastcode='DU' then do;                  
   rc=delobs(dsid_old);                     
   rc=append(dsid_old,'noset');             
   lastcode='AD';                           
end;
                                       
   /* update w/ current values from MASTER.                  */
else rc=update(dsid_old);                  
return;                                                                            


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


JRN_MAIN:                            
   /* return command in uppercase                            */ 
command=word(1,'u');
                 
   /* look at first 3 chars                                  */       
command=substr(command,1,3);         
if lastcode='AD' then addflag=1;     
lastcode=' ';                        
select(command);
                     
   /* user deleted record                                    */       
when('DEL')                          
   code='D ';
                         
      /* update the JOURNAL data set                         */ 
   link WRITEJRN;                     

when('CAN')                          
   code='  ';                         
when('ADD')                          
   code='C ';                         
   lastcode='AD';                     

when('DUP')               
   code='C ';              
   lastcode='DU';          

otherwise                 
   code='C ';              
end;  /* select clause                                       */ 

return;


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


JRN_TERM:                                 
   /* check flag and set code for adds                       */
if addflag then code='AD';               
addflag=0;                                                 

WRITEJRN:                                              
   /* Create the JOURNAL observation if necessary            */    
datetime=datetime();                                  

   /* update with current screen values                      */             
rc=update(dsid_chg);                                  

do _i=1 to _nvar;                                     
if vartype(dsid_old,_i)='N' then do;                  
   _num1=getvarn(dsid_old,_i);                        
   _num2=getvarn(dsid_chg,_i);                        
      /* let code reflect numeric change                     */             
   substr(code,2,1)='N';                              
end;                                                  
else do;                                              
   _char1=getvarc(dsid_old,_i);                       
   _char2=getvarc(dsid_chg,_i);                       

      /* let code reflect character change                   */             
   substr(code,2,1)='C';                              
end;                                                  
if (_char1 ¬= _char2) or (_num1 ¬= _num2) then do;    
   rc=append(journal);                                 
   call putvarc(journal,num_chng,varname(dsid_old,_i));
   if _char1 ¬= _char2 then do;                        

         /* save old value                                   */                           
      call putvarc(journal,num_oldc,_char1);           

         /* save new value                                   */                           
      call putvarc(journal,num_newc,_char2);           
      _char1=' '; _char2=' ';                         
   end;
   else do;                                     

         /* save old value                                   */                    
      call putvarn(journal,num_oldn,_num1);      

         /* save new value                                   */                    
      call putvarn(journal,num_newn,_num2);      
      _num1=.;  _num2=.;                         
   end;                                         

      /* add other variables that are not in                 */
      /* changes data set if new observation                 */
   if substr(code,1,1) = 'A' then               

         /* get key from changes                             */           
      key1=getvarc(dsid_chg,numokey1);          

      /* else key from old_recs                              */              
   else key1=getvarc(dsid_old,numokey1);        
   call putvarc(journal,num_key1,key1);         
   call putvarc(journal,num_code,code);         
   rc=update(journal);                          
   end;                                           
end;                                            
return;                                                      


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


JRNFTERM:                                    
      /* close data sets, FSEDIT terminates                  */
   rc=close(journal);                         
   rc=close(dsid_old);                        
   rc=close(dsid_chg);                        

      /* delete temporary work data sets                     */
   rc=delete('old_recs');                     
   rc=delete('changes');                      
return;


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


FSEINIT:
         call symput('journal','save.journal');   
         call symput('master','save.inventory');  
         link JRNFINIT;                           

        /* any other application specific code               */
                                                 
return;                                           

INIT: link JRN_INIT;                              

         /* any other application specific code              */
                                                 
return;                                           

MAIN: link JRN_MAIN;                              

        /* any other application specific code               */
                                                 
return;                                           

TERM: link JRN_TERM;                              

        /* any other application specific code               */ 
                                                 
return;                                           

FSETERM: link JRNFTERM;                           

        /* any other application specific code               */ 
                                                 
return;                                           

%JOURNAL;                                         


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