Formulas are used to define the categories of new variables and to recode the categories of existing variables. Formulas for recoding existing categorical variables (using the 'Recode' command) are simple to use. Formulas for recoding numerical variables and for creating new variables (using the 'Add new variable' command) are more flexible and can be as complex as you need.
+ When recoding variables using the 'Recode variable' button in the Edit toolbar, you can use the Custom formula field to define the recode categories
The valid formats for recoding categorical variables are:
(old values 1), new value 1, (old values 2), new value 2 etc.
(old values )=1, new value 1, (old values) =2, etc.
An example of customized formula is: "(1,2),1,(3,4,),2", which recode a categorical variable with four categories into a variable with two categories (example below).
+ When Recoding variables using the command 'Add a new (calculated) variable' in the formula field, you can basically define a new variable.
The formula field uses Visual Basic scripting language. A full description of Visual Basic scripting is beyond the scope of this tutorial, but the most commonly used formula types are
+ the "Label" function
The label function allows you to define categories in a variable. The basic format of the label function in the formula field is:
Label( (definition of value 1), value 1, (definition of value 2),2,etc. )
To define a value, you can use variable names and operators and functions such as "in".
The formula is always evaluated in the order in which it is written, and each case is only used once. Cases that fall into the first category, for instance, will not fall into later categories.
When a "1" is inserted in the place of a definition of a value, this is evaluated as always true. This can be useful when defining an 'other' category at the end of a formula. A variable name can also be used instead of a value. DataDynamic Reporter will return the values of that variable for those cases that fall into that particular category.
+ the "In" function
A useful command in formulas is the "in" command. With the "in" command you can find one or more values in a variable, or a value in one or more variables.
The 2 formats admitted are:
in(variable name, value 1, value 2, value 3 etc.)
in(value, variable name 1, variable name 2, variable name 3 etc.)
For instance, you want to define a value 1 in a new variable, based on the occurrence of a value 6 or 7 in the variable VAR1, the definition would look like this:
It is also possible to look for a value within a number of variables. An example is: looking for the value 6 in variables VAR1, VAR2 and VAR3. Using the in command, this would look like this:
The "in" command is always part of a "Label" command.
+ the "If" function'.
The "if" function can be used in formulas to return a conditional value. The format is:
if(condition, value if true, value if false).
Check the online manual for a further description and for some examples.