DECLARE

Topics:

How to:

Reference:

The DECLARE command creates global and local variables (including objects), and gives you the option of assigning an initial value.

Where you place a DECLARE command within a procedure depends on whether you want it to define local or global variables. See Local and Global Declarations for more information.

Syntax: How to Use the DECLARE Command

The syntax of the DECLARE command is

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

where:

objectname

Is the name of the object or other variable that you are creating. The name is subject to the standard naming rules of the Maintain Data language. See Specifying Names for more information.

datatype

Is a data type (a class or built-in format).

expression

Is an optional expression that will provide the initial value of the variable. If the expression is omitted, the initial value of the variable is the default for that data type: a space or null for character and date and time data types, and zero (0) or null for numeric data types. When declaring a new variable using a class data type, you must omit expression.

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.

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.

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.

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

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 is null, the target variable will be null.

DATA

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

( )

Groups a sequence of declarations into a single DECLARE command. The parentheses are required for groups of local declarations, otherwise they are optional.

Reference: Commands Related to DECLARE

  • DESCRIBE. Defines classes and data type synonyms.
  • COMPUTE. Creates global variables, including objects, and assigns values to existing variables.

Local and Global Declarations

When you declare a new variable, you choose between making the variable:

  • Local. To declare a local variable, issue the DECLARE command inside the desired function. The DECLARE command must precede all other commands in the function. A local variable is known only to the function in which it is declared.

    To declare a local variable in the Top function, note that you cannot issue a DECLARE command in an implied Top function, but you can issue it within an explicit Top function.

  • Global. To declare a global variable, place the DECLARE command outside of a function (for example, at the beginning of the procedure prior to all functions), or define it using the COMPUTE command anywhere in the procedure. Note that if you place any DECLARE commands at the beginning of the procedure, you must have an explicit Top case in order to end the global declarations. A global variable is known to all the functions in the procedure.

We recommend declaring your variables locally, and to work with a variable outside the function in which it was declared, passing it to the other function as an argument. Local variables are preferable to global variables because they are protected from unintended changes made in other functions.

WebFOCUS

Feedback