Introduction
This article describes the difference between Rules and QScripts which both allow the automation and manipulation of outputs in Q.
1. Rules
A Rule is essentially a set of instructions about how to manipulate a particular output (see How to Use Rules in Q for more detail). At its simplest, it may be something like automatically shading some cells. However, rules can be used to do much more complex things, such as estimating advanced statistical models.
Irrespective of precisely what a rule does, its endpoint is always the same: the specific outputs that that the rule has been applied to are altered in some way.
2. QScript Macros
QScript is used to automate things. For example, it may be used to:
- Automatically create lots of tables.
- Automatically create new variables that represent top 2 box scores.
Sometimes QScript is even used to automate the application of a rule. For example, Regression - Legacy Driver Analysis - Table of Shapley Importance Scores uses a QScript to automatically create new questions and variables in the data and then uses a rule to appropriately analyse this data.
Note that while a QScript can use a rule, the reverse is not possible (i.e., a rule cannot run a QScript).
3. The use of JavaScript
Although rules and QScripts do different things, they are closely related as both use JavaScript and both can be used to access and manipulate data in Q.
When and when not to use them
Rules
In general, rules should only be used when there is no other way to achieve the same outcome. For example, a rule should never be used to compute a top 2 box score, as there are lots of other ways to do the same thing in Q. And, these other ways are generally better in that a rule merely modifies the output, whereas the other methods are tightly integrated into everything in Q (e.g., if you compute a top 2 box score by dragging and dropping, but when you update your data file the labels have changed, Q will alert you to the problem; by contrast, if you use a rule that is simply adding the 4th and 5th columns of a table you may never notice that the analysis is broken).
QScript
In general, QScript should only be used when you are trying to automate something. Basic data manipulation, such as modifying a specific variable or updating data files, is generally more speedily and accurately performed using in-built tools in Q. This is even the case for power users,
Comparison to SPSS and Excel
The relationship between rules and QScripts is essentially the same as the relationship between conditional formatting and macros in Excel (i.e., where conditional formatting is a type of rule and Excel Macros perform the same role as QScript Macros).
When working in SPSS, just about all syntax corresponds to things that are done in QScript or via the menus in Q (i.e., although there are some equivalents to rules in SPSS, such as the highlighting of cells in tables when significant, they are used by very few users of SPSS and even rarer still are controlled via SPSS Syntax). Whereas an expert SPSS user will tend to make much use of syntax in SPSS, the same user will generally be more efficient if using no QScript (i.e., QScript is only for very advanced things, and it is a mistake to think that because you are a wiz in syntax in SPSS that you should use QScript for the same things).