Hide part of an account number in ILE RPG using %len %trim and %subst

Here is some example code to hide part of an account number using built-in functions %len, %trim, and %subst.

This code snippet puts asterisks in the first four positions of an account number unless the account number is four or less positions. When a small account number is found the number is appended to the ****.

Example:   Acct# 1234Long#123456      becomes ****Long#123456
However    Acct# 5678                 becomes ****5678

This method is useful whenever it is undesirable to display full account numbers or personal identification numbers.  This example also makes use of monitor and on-error (which works a lot like monmsg but is coded within the RPGLE program).

077.00 c   ZZKEY           CHAIN     PYPZZ                        
078.00 c                   IF         NOT %found(PYPZZ)
080.00 c                   eval      zzACT =   *BLANKS 
        *                        
081.00  *b: Do not print full acct#

        *                                                     
082.00 c                   else

082.01 c                   select                                    
083.00 c                   when        %len(%trim(zzact))<(4+1)          
084.00 c                   eval     
zzact='****'+%trim(zzact) 

085.00 c                   other                       
086.00 c                   monitor                                    
087.00 c                   EVAL      zzACT='****'+                   
088.00 c                     
%subst(%trim(zzact):%len(%trim(zzact))-4-1))

089.00 c                   on-error                                   
090.00 c                   endmon
091.00 c                   endsl

091.01 c                   endif
        *                                                 
092.00  *e: Do not print full acct# 


Note that additional code (i.e. a validation routine or an error handling subroutine calls) can exist between the on-error and endmon. When the endmon immediately follows on-error this is equivalent of the CLLE MONMSG MSG(CPF0000) where all messages are trapped and the program can continue.