COMPRESS

  COMPRESS [NUMERIC] [FULL]
 

operand1 [(parameter)]
SUBSTRING (operand1,operand3,operand4) [(parameter)]
 
  INTO operand2

 
SUBSTRING (operand2,operand5,operand6)
 
  LEAVING [SPACE]  
LEAVING NO [SPACE]  
WITH [ALL] [DELIMITERS] [operand7]  

This document covers the following topics:

For an explanation of the symbols used in the syntax diagram, see Syntax Symbols.

Related Statements: ASSIGN | COMPUTE | EXAMINE | MOVE | MOVE ALL | SEPARATE

Belongs to Function Group: Arithmetic and Data Movement Operations


COMPRESS Usage

The COMPRESS statement is used to transfer (combine) the contents of one or more operands into a single field.

COMPRESS Syntax Description

Operand Definition Table:

Operand Possible Structure Possible Formats Referencing Permitted Dynamic Definition
operand1 C S A G N A U N P I F B D T  L   G O yes no
operand2   S       A U         B             yes yes
operand3 C S           N P I   B*             yes no
operand4 C S           N P I   B*             yes no
operand5 C S           N P I   B*             yes no
operand6 C S           N P I   B*             yes no
operand7 C S       A U         B             yes no

* Format B of operand3, operand4, operand5 and operand6 may be used only with a length of less than or equal to 4.

Syntax Element Description:

Syntax Element Description
NUMERIC
Handling of Sign Characters:

This option determines how sign characters and decimal characters are to be handled:

Without NUMERIC, decimal points and signs in numeric source values are suppressed before the values are transferred. For example:

COMPRESS -123  1.23 INTO #TARGET WITH DELIMITER '*'
Content of #TARGET is: 123*123

With NUMERIC, decimal points and signs in numeric source values are also transferred to the target field.

For floating point source values, decimal points and signs are transferred, regardless of whether NUMERIC has been specified or not.

Example 1:
COMPRESS NUMERIC -123  1.23 INTO #TARGET WITH DELIMITER '*'
Content of #TARGET is: -123*1.23
Example 2:
COMPRESS NUMERIC 'ABC' -0056.00 -0056.10 -0056.01 
INTO #TARGET WITH DELIMITER '*'
Content of #TARGET is: ABC*-56*-56.1*-56.01
Example 3:
COMPRESS NUMERIC FULL 'ABC' -0056.00 -0056.10 -0056.01 
INTO #TARGET WITH DELIMITER '*'
Content of #TARGET is: ABC*-0056.00*-0056.10*-0056.01
FULL
Handling of Source Field Values:

Without FULL, the following are removed from the source fields before the values are transferred:

  • leading zeros before the decimal point for fields of format N, P or I

  • trailing zeros after the decimal point for fields of format N or P

  • trailing blanks for fields of format A

  • and leading binary zeros for fields of format B

For a numeric source field containing all zeros, one zero will be transferred. For example:

COMPRESS 'ABC '  001 INTO #TARGET WITH DELIMITER '*'
Content of #TARGET is: ABC*1

With FULL, the values of the source fields in their actual lengths will be transferred to the target field. In other words:

  • leading zeros before the decimal point for fields of format N, P or I

  • trailing zeros after the decimal point for fields of format N or P

  • and trailing blanks for fields of format A

  • leading binary zeros for fields of format B

are displayed as entered. For example:

COMPRESS FULL 'ABC '  001 INTO #TARGET WITH DELIMITER '*'
Content of #TARGET is: ABC *001
operand1
Source Fields:

As operand1, you specify the fields whose contents are to be transferred.

Note:
If operand1 is not of format A or B, its content is converted into alphanumeric representation before it is transferred. If necessary, the alphanumeric representation is truncated.

Using operand1 without an explicit Edit Mask, a ...

- Time variable (format T) is transferred only with the time component, not the date component.
- Logical variable (format L) with value <false> is represented by a blank and value <true> is represented by char "X".

operand2
Target Field:

As operand2, you specify the field which is to receive the values of the source fields.

If the target field is of format U (Unicode) and if a source field of format B is involved, the length of the sending binary field must be even.

LEAVING SPACE
Values in Target Field Separated by a Blank:
If you use the COMPRESS statement without any further options, or if you specify LEAVING SPACE (which also applies by default), the values in the target field will be separated from one another by a blank.
LEAVING NO SPACE
Values in Target Field Not Separated:
If you specify LEAVING NO SPACE, the values in the target field will not be separated from one another by a blank or any other character.
parameter
Print Mode/Date Format/Edit Mask Parameters:
As parameter, you can specify the session parameters PM, DF, EM, or EMU:
PM=I

In order to support languages whose writing direction is from right to left, you can specify PM=I so as to transfer the value of operand1 in inverse (right-to-left) direction to operand2. For example, as a result of the following statements, the content of #B would be ZYXABC:

MOVE 'XYZ' TO #A
COMPRESS #A (PM=I) 'ABC' 
INTO #B LEAVING NO SPACE

Any trailing blanks in operand1 will be removed (except if FULL is specified), then the value is reversed character by character and transferred to operand2.

DF If operand1 is a date variable, you can specify the session parameter DF as parameter for this variable.
EM=
Edit Mask:
For details on edit masks, see the session parameter EM in the Parameter Reference. The EM parameter cannot be applied for group operands or when the SUBSTRING option is used.
EMU=
Unicode Edit Mask:
For details on Unicode edit masks, see the session parameter EMU in the Parameter Reference. The EMU parameter cannot be applied for group operands or when the SUBSTRING option is used.
SUBSTRING (operand1, operand3, operand4)
SUBSTRING Option:
If operand1 is of alphanumeric (A), Unicode (U) or binary (B) format, you can use the SUBSTRING option to transfer only a certain part of a source field. After the field name (operand1) you specify first the starting position (operand3) and then the length (operand4) of the field portion to be transferred.
INTO SUBSTRING (operand2, operand5, operand6)
INTO Clause:

Also, you can use the SUBSTRING option in the INTO clause to transfer source values into a certain part of the target field.

In both cases, the use of the SUBSTRING option in a COMPRESS statement corresponds to that in a MOVE statement. See the MOVE statement for details on the SUBSTRING option.

WITH DELIMITERS
Input Delimiter Character:

If you wish the values in the target field to be separated from one another by a specific character, you use the DELIMITERS option.

If you specify WITH DELIMITERS without operand7, the values will be separated by the input delimiter character as defined with the session parameter ID.

WITH DELIMITERS operand7
Specific Delimiter Character:
If you specify WITH DELIMITERS operand7, the values will be separated by the character specified with operand7. operand7 must be a single character. If operand7 is a variable, it must be of format/length (A1) or (B1).

If the target field is of format A or B, the format/length of the delimiter has to be (A1), (B1) or (U1).

If the target field is of format U (Unicode), the format/length of the delimiter has to be (A1), (B2) or (U1).

WITH ALL
Handling of Delimiters:

Without ALL, a delimiter is placed in the target field only between values actually transferred. For example:

COMPRESS 'A' ' ' 'C' ' ' INTO #TARGET WITH DELIMITERS '*'
Content of #TARGET is: A*C

With ALL, a delimiter is also placed in the target field for each blank value that is not actually transferred. This means that the number of delimiters in the target field corresponds to the number of source fields minus 1. This may be useful, for example, if the content of the target field is to be separated again with a subsequent SEPARATE statement. For example:

COMPRESS 'A' ' ' 'C' ' ' INTO #TARGET WITH ALL DELIMITERS '*'
Content of #TARGET is: A**C*

COMPRESS Processing

A destination field of format B is handled like a destination field of format A.

The COMPRESS operation terminates when either all operands have been processed or the target field (operand2) is filled.

If the target field contains more positions than all operands combined, all remaining positions of operand2 will be filled with blanks. If the target field is shorter, the value will be truncated.

If operand2 is a dynamic variable, the COMPRESS operation terminates when all source operands have been processed. No truncation will be performed. The length of operand2 after the COMPRESS operation will correspond to the combined length of the source operands. The current length of a dynamic variable can be ascertained by using the system variable *LENGTH.

COMPRESS Examples

This section covers the following topics:

Example 1 - Compress

** Example 'CMPEX1': COMPRESS                                           
************************************************************************
DEFINE DATA LOCAL                                                       
1 EMPLOY-VIEW VIEW OF EMPLOYEES                                         
  2 NAME                                                                
  2 FIRST-NAME                                                          
  2 MIDDLE-I                                                            
*                                                                       
1 #COMPRESSED-NAME (A20)                                                
END-DEFINE                                                              
*                                                                       
LIMIT 4                                                                 
READ EMPLOY-VIEW BY NAME                                                
  COMPRESS FIRST-NAME MIDDLE-I NAME INTO #COMPRESSED-NAME     
  DISPLAY NOTITLE                                                       
          FIRST-NAME MIDDLE-I NAME 5X #COMPRESSED-NAME                  
END-READ                                                                
*                                                                       
END

Output of Program CMPEX1:

     FIRST-NAME      MIDDLE-I         NAME               #COMPRESSED-NAME  
-------------------- -------- --------------------     --------------------
                                                                           
KEPA                          ABELLAN                  KEPA ABELLAN        
ROBERT               W        ACHIESON                 ROBERT W ACHIESON   
SIMONE                        ADAM                     SIMONE ADAM         
JEFF                 H        ADKINSON                 JEFF H ADKINSON

Example 2 - Compress Leaving No Space

** Example 'CMPEX2': COMPRESS (with LEAVING NO SPACE)                   
************************************************************************
DEFINE DATA LOCAL                                                       
1 EMPL-VIEW VIEW OF EMPLOYEES                                           
  2 NAME                                                                
  2 CURR-CODE (1)                                                       
  2 SALARY    (1)                                                       
*                                                                       
1 #CCSALARY   (A20)                                                     
END-DEFINE                                                              
*                                                                       
LIMIT 4                                                                 
READ EMPL-VIEW BY NAME                                                  
 
  COMPRESS CURR-CODE (1) SALARY (1) INTO #CCSALARY                      
           LEAVING NO SPACE                                            
  DISPLAY NOTITLE                                                       
          NAME CURR-CODE (1) SALARY (1) 5X #CCSALARY                    
END-READ                                                                
*
END

Output of Program CMPEX2:

        NAME         CURRENCY   ANNUAL            #CCSALARY      
                       CODE     SALARY                           
-------------------- -------- ----------     --------------------
                                                                 
ABELLAN              PTA         1450000     PTA1450000          
ACHIESON             UKL           11300     UKL11300            
ADAM                 FRA          159980     FRA159980           
ADKINSON             USD           34500     USD34500

Example 3 - Compress with Delimiter

** Example 'CMPEX3': COMPRESS (with delimiter)                          
************************************************************************
DEFINE DATA LOCAL                                                       
1 EMPL-VIEW VIEW OF EMPLOYEES                                           
  2 NAME                                                                
  2 CURR-CODE (1)                                                       
  2 SALARY    (1)                                                       
*                                                                       
1 #CCSALARY   (A20)                                                     
END-DEFINE                                                              
*                                                                       
LIMIT 4                                                                 
READ EMPL-VIEW BY NAME                                                  
  COMPRESS CURR-CODE (1) SALARY (1) INTO #CCSALARY                      
           WITH DELIMITER '*'
  DISPLAY NOTITLE NAME CURR-CODE (1) SALARY (1) 5X #CCSALARY            
END-READ                                                                
*                                                                       
END

Output of Program CMPEX3:

        NAME         CURRENCY   ANNUAL            #CCSALARY      
                       CODE     SALARY                           
-------------------- -------- ----------     --------------------
                                                                 
ABELLAN              PTA         1450000     PTA*1450000         
ACHIESON             UKL           11300     UKL*11300           
ADAM                 FRA          159980     FRA*159980          
ADKINSON             USD           34500     USD*34500

Example 4 - Compress with Edit Mask EM

** Example 'CMPEX4': COMPRESS (with edit mask EM)                         
************************************************************************
DEFINE DATA LOCAL                                                       
1 #A10     (A10)   INIT <'ABCDEF'>                                      
1 #I4      (I4)    INIT <-123>                                          
1 #T       (T)     INIT <E'2021-11-22 10:24:36'>                        
1 #L       (L)     INIT <TRUE>                                          
1 #RESULT  (A70)                                                        
END-DEFINE                                                              
*                                                                       
COMPRESS '#A:'  #A10  (EM=X_X_X)                                        
         '#I4:' #I4   (EM=-999Z)                                        
         '#T:'  #T    (EM=YYYY-MM-DD_HH:II)                             
         '#L:'  #L    (EM=FALSE/TRUE)       INTO #RESULT                
PRINT #RESULT                                                           
END

Output of Program CMPEX4:

#A: A_B_C #I4: -0123 #T: 2021-11-22_10:24 #L: TRUE