Announcement

Collapse
No announcement yet.

Numeric Date Check Macro

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Numeric Date Check Macro

    Numeric Date Check Macro

    This macro contains SCL code which can be used on an objects onLostFocus() code to validate that field for a SAS Date Value. It can be called as a soft or hard edit and allows you to specify upper and lower date boundries. If no upper bound is set, then any date in the future will be in error.

    Below is the macro code - this can also be downloaded from the attachment at the bottom of this post or by clicking here.

    %SCLNUMDATE
    Code:
     
    /*************************************************************************\
       Macro: SCLNUMDATE
       
       Programmer:  G. Wagner
       Date: 06/14/05
          
       Purpose: 
          SCL Code which validates a field for a valid SAS Date value 
          within range specified or not in the future.  Messages values will
          inherit the objects date format (mmddyy10., date9 etc...) to be 
          displayed to the user.
    
       Paramters / Variables:  
          Since this code is resolved and compiled in the SCL code when the
          screen is compiled, these values are just set as scl variables in 
          the method code.
             
                SOFTCHECK 
                   Num - 1,0.  If set to 1 a message box is displayed 
                   allowing allowing user to accept or decline the value
                   entered.  If set to 0, they must correct an invalid 
                   value.   
                   
                ALLOWEDVALUES - 
                   String - IF SET WILL NOT ERROR. For example, to allow
                   a special mising value of .U to be entered, specify a 
                   U in the string.
                
                LOWDATE
                   String - Specify the low date boundary in MMDDYY10 format.
                   
                HIGHDATE
                   String - Specify the upper boundary in MMDDYY10 format.  
                   If no highdate is specified, then any date in the future 
                   is in error.
       
    \*************************************************************************/
    %MACRO SCLNUMDATE;
       
       ** intialize softCheck so it does not throw a warning if not specified in object code **;
       softCheck=softCheck;
       allowedValues=allowedValues;
       
       ** initialize low and high date variables;
       lowdate=lowdate;
       highdate=highdate;
          
       ** initialize an error flag variable **;
       hasError=0;
    
       if _self_.text > . then do;
        
          ** Check for allowed values **;
          if allowedValues > '' and _self_.text < = input('.Z',2.) then do;
             if indexw(allowedValues,_self_.text) then return;
             else do;
                hasError=1;
                _self_.cpErrorMessage='The value specified is not in the allowed values ('||allowedValues||').';                  
             end;
          end;
    
          if hasError then leave;
       
          ** Set low and high values as numerics **;
          if lowdate>'' then lowdaten=input(lowdate,mmddyy10.);
          if highdate>'' then highdaten=input(highdate,mmddyy10.);
                
          if lowdate > '' then do;
             ** Date is less than low value **;
             if lowdaten > _self_.text then do;
                hasError=1;
                _self_.cpErrorMessage='The value specified ('||putn(_self_.text,_self_.format)||') is less than '||putn(lowdaten,_self_.format)||'.';        
             end;
          end;
          
          if hasError then leave;
          
          if highdate > '' then do;
             ** Date is greater than high value **;
             if highdaten < _self_.text then do;
                hasError=1;
                _self_.cpErrorMessage='The value specified ('||putn(_self_.text,_self_.format)||') is greater than '||putn(highdaten,_self_.format)||'.';       
             end;
          end;
          else do;
             ** Date is in Future **;
             if _self_.text > date() then do;       
                hasError=1;
                _self_.cpErrorMessage='The value specified ('||putn(_self_.text,_self_.format)||') is in the future.';         
             end;        
          end;
    
       end;
    
       ** Check for hard or soft error **;
       if hasError then do; 
          if softCheck then do;
             msglist={};
             rc=rc;
             rc=insertc(msglist,_self_.cpErrorMessage,-1);
             rc=insertc(msglist,'Is this correct?',-1);
             dcl char rcc=messageBox(msglist,'I','YN','Question','N');
             if rcc='NO' then do;
                _self_._errorOn();
                return;
             end;
             msglist=dellist(msglist,'y');
          end;
          else do;
             _self_._errorOn();
             return;
          end;
       end;
          
    %MEND SCLNUMDATE;
    Calling the macro
    In the onLostFocus() method for the date field to be checked, specify the values and call the macro as follows:
    Code:
    /** OnLostFocus Method **/
    
    %include objfile;
    
    _OnLostFocus: method _self_:u:object;
    link createobj;
    ************************;
    ** Add user code here **;
    ************************;
    
    ** Specify soft (0) or hard (1) edit **;
    softcheck=0;
    
    ** specify the special valid values to allow separated by spaces **;
    allowedValues='U A';
    
    ** specify the lower date bound if required **;
    lowdate='01/01/2000';
    
    ** specify the upper date bound if required **;
    highdate='';
    
    ** Call date macro **;
    %sclnumdate;
    
    
    endmethod;
    Attached Files
    Last edited by Ronni Rubenstein; 12-21-2009, 11:25 AM.
Working...
X