Table logic (or tablogic) allows you to add CSPro logic that is executed during tabulation. This is mainly used to add your own "recoded" variables to tables instead of using existing variables from your dictionary. This is useful when the categories you want in your table must be tallied based on the values of more than one variable from the dictionary, in other words, when the categories you want must be computed based on the values of multiple variables. In such a case, you can create a new variable with the categories you want and write logic to set its value based on the values of existing variables.
For example, if you want to tabulate houses that have "complete plumbing," meaning that they have piped water, bathing facilities and a toilet inside the housing unit. The categories for "complete plumbing" are:
Complete – piped water inside the unit, and a private toilet inside the unit and bathing facilities inside the unit.
Some but not all –one or more of the three conditions above, but not all three.
None – none of the above conditions.
The variable "complete plumbing" does not exist in the main dictionary. It can be however, be determined based on the values of the following three variables that are in the dictionary:
Rather than adding new variables to your existing dictionary, you can add new variables to the working storage dictionary. The working storage dictionary is a second dictionary that is created automatically when you create a tabulation application. This dictionary is similar to other dictionaries, however there is no data file associated with it. The variables in the working storage dictionary must be set by program logic.
In this example, we will add the new "complete plumbing variable" to the working storage dictionary. The working storage dictionary appears just below the main dictionary in the dictionary tree. Adding a new variable to the working storage dictionary is the same as adding a variable to any dictionary. Right-click on the "Working Storage Record" under the working storage dictionary in the dictionary tree and choose "Add Item".
Then fill in the label, name and other properties for the new variable.
Next, create the value set for the new variable.
Now drag the new variable onto the table just as if it were a variable in the main dictionary.
Finally add the tablogic to set the value of complete plumbing based on the values of source of water, toilet facilities and bathing facilities. Bring up the Tally Attributes (Table) dialog and enter the following code in the tab logic edit box:
The above logic will be executed once for each housing record to set the value of complete plumbing for that household. This will result in the following table:
When using variables from the working storage dictionary, it is important to pay attention to the unit of tabulation. When using variables from the main dictionary, CSPro can determine the unit of tabulation based on the variables used. If you choose variables from the Person record, such as sex, the unit will be set to the person record. If you choose variables from the housing record, the unit will be set to the housing record. However, when you drag variables from the working storage dictionary onto the table, CSPro does not know which record or records the recodes for these variables will come from. It always sets the unit of tabulation to the first level (questionnaire). In the above example, this does not cause any problems because there is exactly one housing record per questionnaire. However, had we created a recode of variables from the person record, we would need to set the unit to the person record, otherwise we could get errors during tabulation. For more information see Changing the Unit of Tabulation.
Note that often it is preferable to create recoded variables in a batch edit program rather than during tabulation. This recode could have been accomplished by adding a new variable to the main dictionary for complete plumbing and writing a batch edit program to set the value of this variable for each case. Then the new complete plumbing variable could be dropped on the table directly, rather than using a working storage variable. The advantage of using batch edit is that the logic to set the value of the new variable is done once in a batch edit program rather than done as tablogic in each table in which the variable is used.
Another alternative is to use multiple subtables and value sets to simulate a recoded variable. This can be easier for simple variables, although for recoded variables with more than a few categories, it is generally simpler to use tablogic or batch edit. For more information see Recodes in Tables Using Value Sets and Subtables.