Operations with macros

Using macros in expressions

To use a macro in a PDL or SRL expression, type the macro’s name as the first argument to the macro() function (see the sections on PDL macro or SRL marco functions for reference). Macro name rules are like column name rules. Make sure to only use PDL-oriented macros in PDL expressions and SRL-oriented macros in SRL expressions.

For example, if you created a PDL macro() named MyMacro, then you would reference the MyMacro macro in your PDL expression by using the PDL function macro(MyMacro).

Suppose the PDL expression for MyMacro was just a simple search for a word, such as "dog". In this case, the expression macro(MyMacro) would be equivalent to dog. Both expressions would match the same records, as all a macro really is in this case is a saved expression.

For help using custom arguments with macros, please contact Megaputer support.

Executing nodes with expressions containing macros

Each time you change a macro, PolyAnalyst does not automatically update (re-execute) any node that contains an expression that references that macro. You will need to re-execute any node referencing a macro each time you modify the macro.

It is recommended to always immediately execute affected nodes after editing your macros in order to avoid confusing situations where the node produces unexpected results but uses the prior version of a macro have you have since edited.

PDL provides the macro() and var() functions to enable you to use macros and variables, respectively, in your PDL expressions. These types of functions are commonly used to shorten your search queries.

Macros and variables can serve several purposes. Notably, using macros and variables makes it easy to reuse certain search query components that you find yourself or your team routinely using in your analyses. In addition, as PDL expressions written within the taxonomy can sometimes grow quite large, using macros and variables helps condense the expressions into a more readable format. This sometimes simplifies the management of several expressions in larger taxonomies.

Macros and variables are automatically exported with the project. Server and user macros and variables are automatically exported with the project only if the user told the system to do so in the project settings. To do this, select the Project settings from the Analytical Client settings and select the Execution setting in the option list to the left.

clone macros

The If macros/var is missing, then setting includes several options. You can either use user/server macros and variables or clone user/server macros and variables by selecting the corresponding option.

The macro() function

The PDL macro() or SRL macro() function replaces the call with macro expansion.

The syntax for the macro() function is macro(name, arguments). The function allows using spaces in arguments, like in macro (x, y, z).

Here is how the macro() function works. Suppose we have a macro with the following properties:

Name: test

Arguments: x; y; z…​

Expansion: pattern(x, z, y)

Call: macro(dog, cat, mouse, horse, cow)

Result: pattern(dog, mouse, horse, cow, cat)

Here "dog" is mapped to the first argument (x), "cat" – to the second argument (y) and the rest of the arguments replace z.

The var() function

The function syntax is var(name of variable).

For example, if you have a variable named myvar, then you could use the variable myvar within your PDL expression by writing var(myvar).

See the sections on SRL var() and PDL var() functions for reference.

Overriding the default columns for variables and macros

Macro and variable expressions each apply to a column based on the context of how the macro or variable is used.

For example, if using a macro within a taxonomy category, then the macro is evaluated against the column specified within the properties of the taxonomy category.

You can, however, change parts of a macro expression to apply to different columns within an input dataset. By default, each component of a PDL expression within a macro applies to the contextually-determined column. This is denoted by the use of an @ symbol followed by the name of the column. If the column name contains spaces or other special characters the column name must be enclosed in [brackets]. If there are no spaces or special characters then the use of brackets is optional. For example:

(word or word)@Column

(word and not otherword)@[Column with spaces in name]

If the expression is not intended to apply to columns other than the contextually-determined column, then there is no need to explicitly add the @ColumnName modifier. The modifier is implicitly added. In fact, the modifier should only be added when you want to deviate from the normal behavior and apply all or a part of an expression to some column other than the default contextually-determined column.

For example, if a taxonomy category is configured to search a column named Column1, then regardless of whether you specify @Column1 at the end of the category’s PDL expression, PolyAnalyst searches Column1 when evaluating the PDL expression.

If you, however, want to search a different column, you can do something like (my expression)@OtherColumnName.

The @Column modifier is applied distributively to the operands of an PDL expression.

For example, (word1 or word2)@Column is equivalent to word1@Column or word2@Column, because the @Column modifier is applied distributively.

There are some caveats to be aware of when specifying the columns to which parts of a PDL expression applies:

  1. column references apply only to columns not already referenced;

  2. second, column references do not replace columns that are already referenced.

In other words, there is some tricky behavior regarding how the column modifier that applies to the whole expression is applies to its individual parts. Specifically, there is some ambiguity regarding what happens when you have an expression like (word1@Column1)@Column2. Which column is searched? Because word1 references Column1 explicitly, Column1 is used, not Column2. In other words, in this case, Column1 takes priority over Column2. In the case of (word1)@Column2, this is equivalent to word1@Column2 after distribution, because word1 was not yet referenced (there was no explicit qualifier/modifier that specified which column to search). For example:

  • (term1@column1 and term2)@column2 means term1@column1 and term2@column2

  • ((term1@column1 and term2)@column2 and term3)@column3 means term1@column1 and term2@column2 and term3@column3

  • ((term1@column1 and term2)@column2)@column3 still means term1@column1 and term2@column2