You are here: Commands > Assign

Assign

You can use the Assign command to store a value as a variable. You can also attach a condition to the Assign command. Only one variable assignment per Assign command. To perform multiple variable assignments in a single command see Let.

Assign is typically used in a procedure in which you want to create or change the value of a variable. If Assign is issued without a condition, the assignment automatically takes place.

Assign can be used as a separate command or within a Group command. An Assign command used within a Group command performs the variable assignment each time the command is encountered, if the test evaluates true for that record at least once per record.

If you assign a value to an existing variable name that is used within a computed field, or the assignment is made inside of a Group command, then the value assigned is adjusted to the specifications previously defined for that variable (if possible); the length is padded or truncated and the decimals are adjusted if required. A standard "adjusting" message used for variable assignments is then displayed in the Command Log.

If you assign a value to a variable name that already exists and the variable is not used in a computed field, or is assigned outside of a Group command, then the previous value and its specifications are overwritten.

For more information on User-Created Variables see User-Created Variables.

Analyzer recognizes several special variables such as HEADER and FOOTER. If character values are assigned to either of these two variables, they are automatically used as page headers or footers if a header or footer is not otherwise specified. This automatic page formatting feature remains in effect until the variables are deleted or until the end of the current session. Be aware of these variables when using the Assign command.

Note: You can also create new and edit existing variables using the Expression Builder.

Analyzer also supports using variables to store an array of values, in either numeric, character, datetime or logical formats. A variable array must use a consistent data type for all array elements; mixing of numeric, character, datetime or logical values is not permitted within the same variable array. Elements in the variable array must have the same format/characteristics (each numeric value must have the same number of decimals and each character string the same (maximum) string length).

Note: The Assign command allows a maximum array size of 1,000,000 elements.

The array value is set by the first assignment to the variable. For example:

x[100]=0.00

y[100]=blanks(100)

would create a variable array called x as an array of 100 numbers with 2 decimals and would create a variable array y as an array of 100 strings with a maximum length of 100 characters.

Tip: Processing variable arrays is always most efficient when the maximum array size is established up front. The easiest way to achieve this is to initially assign a default format and data type to the largest element number in the variable array (as shown in the previous example).

If subsequent variable assignments to array elements are made that don't match the previous definition then the result is forced to match (if possible) and the standard "adjusting" message used for variable assignments within the Group command is displayed in the Command Log.

For more information on Variables Arrays see Variable Arrays.

Parameters

The Assign command has the following parameter: If. For a description, see Command Parameters.

Command Mode Syntax

<ASSIGN> variable-name = expression <IF test>

or

ASSIGN variable-name:literal-string

or

<ASSIGN> variable-name<[n]> = expression <IF test>

variable-name = expression is interpreted automatically as an assignment.
variable-name specifies the name of the variable to be created. All field naming conventions apply. See Naming Fields for details.
expression specifies the value to be assigned to the variable. All Analyzer expression conventions apply.
[n] is an optional parameter for referencing elements in a variable array

The ASSIGN keyword is optional in the first example of the command syntax, because the “=” sign is the assignment operator in Analyzer.

The ASSIGN keyword is mandatory in the second example of the command syntax using the colon “:”. Use of the colon “:” assigns the literal string in its entirety to the variable regardless of quotes. This is useful in an expression where there may be a mix of single and double quotes used (such as is commonly used in a SQL Select Statement).

Note: When using the syntax with the colon, the ASSIGN command cannot be conditionalized as the remainder of the line after the literal string is not parsed, and therefore the IF parameter would not be processed.

Note: When using the syntax with the colon, the assignment cannot include any trailing blanks as these will be removed before the ASSIGN command is parsed.

Examples

In the first example, a user created variable called MPRODCLS is assigned the product class of the current record by entering the command:

ASSIGN MPRODCLS = PRODCLS

Because MPRODCLS is a variable, its value will not change unless explicitly changed by another Assign command.

In the second example, a variable called SAMPLE_QTY will be assigned a value of 1 in a procedure whenever the variable COUNT has a value that is less than 10:

ASSIGN SAMPLE_QTY = 1 IF COUNT < 10

If COUNT is greater than or equal to 10, no action is taken by Analyzer. This means that the value of SAMPLE_QTY remains unchanged. If the variable did not previously exist, it will not be created.

In the third example, a variable called IF_VALUE is assigned a complex condition for use in a subsequent SQL Select Statement using Analyzer's SQL command:

ASSIGN IF_VALUE:"COUNTRY='CANADA'"

In the preceding example , the entire literal string (including all single and double quotes) is stored in the IF_VALUE variable.

In the final example, a variable array called AMOUNT is assigned one hundred elements with a numeric value of 0.00 and then the first element of the variable array is assigned a value of 123.45

ASSIGN AMOUNT[100]=0.00

ASSIGN AMOUNT[1]=123.45

Tip: The keyword ASSIGN can be omitted and the examples above will generate the same result.