Basic Form of the WRITE TO Statement

To write a value (literal) or the contents of a source field to a target field, you use the WRITE TO statement:

Syntax

WRITE <f1> TO <f2> [<option>].

The WRITE TO statement writes the contents of a source field <f1>, which can be any data object, to a target field <f2>, which must be a variable. <f2> cannot be a literal or a constant. The contents of <f1> remain unchanged.

For <option>, you can use all formatting options of the WRITE statement except UNDER and NO-GAP (see Formatting Options).

The WRITE TO statement always checks the settings in the user’s master record. These specify, for example, whether the decimal point appears as a period (.) or a comma (,).

The WRITE TO statement does not follow the conversion rules described in Type Conversions. The target field is interpreted as a type C field. The system always converts the contents of the source field to type C. It then writes the resulting character string to the target field without converting it to the data type of the target field. Therefore, you should not use a target field with a numeric data type.

DATA: NUMBER TYPE F VALUE '4.3',
TEXT(10),
FLOAT TYPE F,
PACK TYPE P DECIMALS 1.

WRITE NUMBER.

WRITE NUMBER TO TEXT EXPONENT 2.
WRITE / TEXT.

WRITE NUMBER TO FLOAT.
WRITE / FLOAT.

WRITE NUMBER TO PACK.
WRITE / PACK.

MOVE NUMBER TO PACK.
WRITE / PACK.

This produces the following output:

4.30000000000000E+00

0.043E+02

1.50454551753894E-153

20342<33452;30,3

             4.3

The first output line displays the contents of the field NUMBER in the standard output format for type F fields. The second output line displays the character string which results from writing the field NUMBER with the formatting option EXPONENT 2 to a type C field with a length of 10. The third and fourth output lines show that it is not feasible to use target fields which are of numeric data type. The fifth output line shows that the MOVE statement works differently than the WRITE TO statement in that the type F field is correctly converted to type P (for further information about this conversion, see Source Type Floating Point Number).