EXPRESSION Statements

Expression statements let you specify an arithmetic expression or a string value. For arithmetic, the program calculates it and places the result in the data location for this question.

Expression questions will usually be labeled since you will likely want to see or check the value you have recorded later during the interview. You should specify a length on EXPRESSION statements. Otherwise the system will use the default width of 9. Specifying the header option NUMERIC_WIDTH_REQUIRED will require that you specify a length.

For numeric expressions, if the result does not fit in the field, the field will be filled with asterisks (*). The maximum width is 20. The format includes up to a 19-digit integer with or without a plus (+) or minus (-) sign, an optional decimal (.), and up to eight decimals of significance. By default, the EXPRESSION statement will look at the expression and determine the maximum number of decimals in any constant or NUMERIC question referenced and use that as the number of decimals to record.

EXPRESSION questions will accept a “number of decimals to save” parameter option similar to NUMERIC questions.

The syntax for an EXPRESSION statement is:

!EXPRESSION,<subtype>,<number of decimals>,expression

The subtype is optional; however, if it is not specified, a comma must be used as a placeholder. A comma placeholder is not required for number of decimals.

The expression may include any combination of the following:

  • Numbers
  • Arithmetic operators
  • References to previous questions or data locations
  • Functions (see USING FUNCTIONS IN CONDITION STATEMENTS)
  • Any number of parentheses (to control the order of execution of the expression)
  • A “quoted string” to save in the data if the subtype is STRING

The arithmetic operators and their order of calculation are:

Operator Type Order of Calculation
** Exponentiation 1
* Multiplication 2
/ Division 2
+ Addition 3
Subtraction 3

 

The order of calculation means that if several operators are used in the same expression without parentheses to prioritize the order, the operator’s type will determine the order. If using two operators with the same order level, the expression is calculated left to right.

Calculations can be done in real arithmetic as well as decimals. The NUMDECS parameter determines how much decimal significance is kept. If any part of the equation is missing, the result will be filled with asterisks.  Expressions are often used to update counters.

Valid Subtypes

  • Blank_Fill
  • Left_Most_Sign_For_ODBC
  • Number_of_Decimals
  • String
  • Zero_Fill

BLANK_FILL

Shortform: EXP,BLANKFILL

This is the default, and right-justifies numbers.  The value will not be zeroed filled (e.g., “ 12”)

EXAMPLE:

{ QTOTAL:
!IF QADDROOM(YES)
!EXPRESSION,BLANK_FILL,NUMITEMS(QROOMS)+1 }

This statement will count the number of responses from the FIELD question QROOMS and add 1 to it if the answer to QADDROOM is YES. The result will be right- justified with leading blanks.

LEFT_MOST_SIGN_FOR_ODBC

Shortform: EXP,LEFTSIGN

Saves enough room for the response to include a plus or minus sign along with the number. This is necessary if converting the data to a SQL database later on.

NUMBER_OF_DECIMALS=

Shortform: EXP,NUMDECS

The NUMBER_OF_DECIMALS option lets you specify the number of decimals the answer will be stored with. It can be a number from 1 to 7, or “NO_DECIMALS” can be entered. If it is 1-7 it stores the value with a decimal point in the data. If you specify NO_DECIMALS, the answer is stored as a rounded whole number, even if the expression has values with decimals in them.

EXAMPLE:

{ QTOTAL:
!EXPRESSION,BLANK_FILL,NUMBER_OF_DECIMALS=2,4*QAPPLE }

This statement will multiply 4 by the answer in QAPPLE, allowing 2 decimal points.  There will be no leading zeroes in the value stored.

EXAMPLE:

{ QTOTAL:
!EXPRESSION,ZERO_FILL,NUMBER_OF_DECIMALS=NO_DECIMALS,(4*QAPPLE)/.25 }

This statement will multiply 4 by the answer in QAPPLE, divide that value by .25, and round to the nearest whole number.  There will be leading zeroes in the value stored.

STRING

Shortform: EXP,STRING

The STRING option will write a “string” to the data instead of a number.

EXAMPLE:

{ QCARNAME: .20
!EXPRESSION,STRING,,”Chevy” }

This statement will save the name “Chevy” into the data. It will also save back references to other questions.

ZERO_FILL

Shortform: EXP,ZERO

The ZERO_FILL option will zero-fill the value and right-justify the numbers. (e.g., “0000012”).

EXAMPLE:

{ QTOTALCARS: [QTOTCARS]
!IF NUMBEROFFORDS>0
!EXPRESSION,ZERO_FILL,QTOTCARS+NUMFORDS }

This statement adds the response to NUMFORDS to QTOTCARS if NUMFORDS had a numeric response greater than zero. The ZERO_FILL subtype stores the data zero-filled. This statement has additional special features. First, it uses the data location previously used by the question QTOTCARS ([QTOTCARS] as the location). This allows you to use the same data space over and over, instead of using a new space for each EXPRESSION question updating QTOTCARS. \|QTOTCARS back references will show the updated number from this point on.

Miscellaneous

You can also use different functions with EXPRESSIONS.

EXAMPLE:

{ QTIMES:
!EXPRESSION,,MAX(WEEK1,WEEK2,WEEK3,5) }

This statement will find the highest numeric response among the questions WEEK1, WEEK2, WEEK3, and the number 5 and return its value.

EXAMPLE:

{ QFAMILY: .3
!EXPRESSION,,X(CHILD)+ X(ADULT) }

This statement will add the responses from questions CHILD and ADULT. If either question is blank or not numeric, the numbers will still be added because the X function causes a missing response to return a zero. Otherwise, if either question were blank, no response would be recorded for this calculation. The answer will be stored in a three-column-wide field.