COMPUTE

Topics:

How to:

Reference:

The COMPUTE command enables you to:

Syntax: How to Use the COMPUTE Command

The syntax of the COMPUTE command is:

[COMPUTE] 
target_variable[/datatype [DFC cc YRT yy] [missing]][= expression];
.
.
.
   missing: [MISSING {ON|OFF} [NEEDS] [SOME|ALL] [DATA]]

where:

COMPUTE

Is an optional keyword. It is required if the preceding command can take an optional semicolon (;) terminator, but was coded without one. In all other situations, it is unnecessary.

When the COMPUTE keyword is required, and there is a sequence of COMPUTE commands, the keyword needs to be specified only once for the sequence, for the first command in the sequence.

target_variable

Is the name of the variable which is being created and/or to which a value is being assigned. A variable name must start with a letter and can only contain letters, numbers, and underscores ( _ ).

datatype

Is included in order to create a new variable. If you are creating a simple variable, you can specify all built-in formats and edit options, except for TX, as described for the Master File FORMAT attribute in the Describing Data With WebFOCUS Language manual. If you are creating an object, you can specify a class. You must specify a data type when you create a new variable. You can only specify the data type of a variable once, and you cannot redefine the data type of an existing variable.

DFC cc

Specifies a default century that will be used to interpret any dates with unspecified centuries in expressions assigned to this variable. cc is a two-digit number indicating the century (for example, 19 would indicate the twentieth century). If this is not specified, it defaults to 19.

Specifying DFC cc is optional if the data type is a built-in format. It is not specified if the data type is a class, as it is relevant only for scalar variables.

YRT yy

Specifies a default threshold year for applying the default century identified in DFC cc. yy is a two-digit number indicating the year. If this is not specified, it defaults to 00.

When the year of the date being evaluated is less than the threshold year, the century of the date being evaluated defaults to the century defined in DFC cc plus one. When the year is equal to or greater than the threshold year, the century of the date being evaluated defaults to the century defined in DFC cc.

Specifying YRT yy is optional if the data type is a built-in format. It is not specified if the data type is a class, as it is relevant only for scalar variables.

missing

Is used to allow or disallow null values. This is optional if the data type is a built-in format. It is not specified if the data type is a class, as it is relevant only for scalar variables.

MISSING

If the MISSING syntax is omitted, the default value of the variable is zero (0) for numeric variables and a space for character and date and time variables. If it is included, its default value is null.

ON

Sets the default value to null.

OFF

Sets the default value to zero (0) or a space.

NEEDS

Is an optional keyword that clarifies the meaning of the command for a reader.

SOME

Indicates that for the target variable to have a value, some (at least one) of the variables in the expression must have a value. If all of the variables in the expression are null, the target variable will be null. This is the default.

ALL

Indicates that for the target variable to have a value, all the variables in the expression must have values. If any of the variables in the expression are null, the target variable will be null.

DATA

Is an optional keyword that clarifies the meaning of the command for a reader.

=

Is optional when COMPUTE is used solely to establish format. The equal sign is required when expression is used.

expression

Is any standard Maintain Data expression, as defined in Expressions Reference. Each expression must end with a semicolon (;). When creating a new variable using a class data type, you must omit expression.

Example: Moving the COMPUTE Keyword

You can place an expression on the same line as the COMPUTE keyword, or on a different line, so that

COMPUTE
TempEmp_ID/A9 = '000000000';

is the same as:

COMPUTE TempEmp_ID/A9 = '000000000';

Example: Multi-Statement COMPUTE Commands

You can type a COMPUTE command over as many lines as you need. You can also specify a series of assignments as long as each expression is ended with a semicolon (;). For example:

COMPUTE TempEmp_ID/A9 = '000000000';
   TempLast_Name/A15 ;
   TempFirst_Name/A10;

Example: Combining Several Statements Into One Line

Several expressions can be placed on one line as long as each expression ends with a semicolon (;). The following shows two COMPUTE expressions on one line and a third COMPUTE on the next line. The first computes a five percent raise and the second increases education hours by eight. The third concatenates two name fields into one field:

COMPUTE Raise/D12.2=Curr_Sal*1.05; Ed_Hrs=Ed_Hrs+8;
Name/A25 = First_Name || Last_Name;

Reference: Usage Notes for COMPUTE

  • If the names of incoming data fields are not listed in the Master File, they must be defined before they can be used. Otherwise, rejected fields are unidentified and the procedure terminates.

    There are two different ways these fields can be defined. The first uses the notation:

    COMPUTE target_variable/format =;

    Because there is no expression after the equal sign (=), the field and its format is made known, but nothing else happens. If this style is used for a field in a form, the field appears on the form without a default value. Because COMPUTE is used solely to establish format, the equal sign is optional and the following syntax is also correct:

    COMPUTE target_variable/format;

    The second method of defining a user-defined field can be used when an initial value is desired. The syntax is:

    COMPUTE target_variable/format = expression;
  • Each field referred to or created in a Maintain Data procedure counts as one field toward the 3,072 field limit, regardless of how often its value is changed by COMPUTE commands. However, if a data source field is read by a Winform command and also has its value changed by a COMPUTE command, it counts as two fields.

Reference: Commands Related to COMPUTE

  • DEFINE. Is a Master File attribute (not a command) that defines temporary fields and derives their values from other fields in the data source. This type of temporary field is called a virtual field. DEFINE automatically creates a corresponding virtual column in every stack that includes the segment of the field. For more information, see the Describing Data With WebFOCUS Language manual.
  • DECLARE. Creates local and global variables.

Using COMPUTE to Call Functions

When you call a function as a separate statement (that is, outside of a larger expression), if the preceding command can take an optional semicolon (;) terminator, but was coded without one, you must call the function in a COMPUTE or PERFORM command. You can use PERFORM for Maintain Data functions only, though not for Maintain Data functions that return a value. For example, in the following source code, the NEXT command does not end with a semicolon (;), so the function that follows it must be called in a COMPUTE command:

NEXT CustID INTO CustStack
COMPUTE VerifyCustID();

However, in all other situations, you can call functions directly, without a COMPUTE command. For example, in the following source code, the NEXT command ends with a semicolon (;), so the function that follows it can be called without a COMPUTE command:

NEXT CustID INTO CustStack;
VerifyCustID();

For more information about terminating commands with a semicolon (;), see Terminating a Command's Syntax.

Using COMPUTE to Dynamically Change the Property of an Object

Instead of using WINFORM SET to dynamically change the property of an object, compute a variable to a value, and assign that variable to the property of the object. When the variable is evaluated, the property of the object is dynamically set.

For example, instead of issuing the following WINFORM SET command:

WINFORM SET FORM.OBJECT.FOCUS TO HERE;

Issue the following COMPUTE command and assign the variable to the FOCUS property of the object.

COMPUTE VAR/I1=1;

where:

VAR

Is the name of the variable used in the dynamic set.

Note: In App Studio Maintain Data, assign the variable to the FOCUS property of the desired object.

WebFOCUS

Feedback