Creating Soft Edits

Hard edits are when you set field properties to force a user to enter a value that is within a range, allowed values, etc. There may be times when you need the user to enter a value that is not within these parameters. The following example is a Soft edit, which will prompt the user that the value falls outside the parameter. This way, they are alerted to the error and can decide if it needs to be corrected. For this, we use the SAS MessageBox object. This code could be used in the onLostFocus, onEnter or onUpdate methods.

Say we want to check that a numeric field must be between 1 and 10, but we want to allow for exceptions, the following process could be used:
  • Evaluate condition
  • Display message box to user
  • If the user responds 'No' then error the field
  • Otherwise, the user can continue without changing the value

The code would look like this:

/** OnLostFocus Method **/                                                                                                                                                                                                                                      
%include objfile;                                                                                                                                                                                                                                               
_OnLostFocus: method _self_:u:object;                                                                                                                                                                                                                           
link createobj;                                                                                                                                                                                                                                                 
** Add user code here **;                                                                                                                                                                                                                                       
    if _self_.text > . and (_self_.text < 1 or _self_.text > 10) then do;                                                                                                                                                                                       
        ** first ask user if correct **;                                                                                                                                                                                                                        
        dcl char msg = 'Is the value specified ('||compress(_self_.text)||') valid?';                                                                                                                                                                           
        rc=insertc(msglist,msg,-1); rc=rc;                                                                                                                                                                                                                      
        dcl char rcc=messageBox(msglist,'?','YN','Question','N');                                                                                                                                                                                               
        ** if no, the error field **;                                                                                                                                                                                                                           
        if rcc='NO' then do;                                                                                                                                                                                                                                    
            _self_.cpErrorMessage='Please specify a valid value.';                                                                                                                                                                                              
The following is the resulting message box.