Examples of inline model variable substitution

Any variable in a model can be used as a variable substitute by enclosing the variable name in percent symbols (%variable name%). This topic has examples of using in-line model variable substitution in ModelBuilder.

Example 1

In the model below, a workspace variable, Data Workspace, has a value of C:\Data.gdb. This workspace location is substituted as an inline variable in the Project tool parameters by enclosing the variable name in percent symbols. At run time, the actual variable value, C:\Data.gdb, is substituted for %Data Workspace%.

Inline data workspace

Example 2

When the iterator Iterate Feature Classes runs, it creates an output variable for both the path and name of the feature class. The value in the Name variable can be used to construct the path to the projected feature class. When the tool executes, %Name% is replaced with the name of the feature class.

Iterator output as inline variable

Example 3

Parcel ID is a model parameter that is specified when the model tool is run. This variable is used in the Expression parameter of the Select Layer By Attribute tool as "Parcel" = '%Parcel ID%'. When the tool runs, %Parcel ID% is replaced with the parcel ID (9 in the case below), and only those parcels with an ID of 9 are selected.

Use of inline variable in Select Layer By Attribute tool
NoteNote:
  • The Parcel ID variable in this example is of data type string. In-line variables of type string need to be enclosed within quotes ('%string variable%') in an expression. In-line variables of type numbers (double, long) do not require quotes (%double%).
  • Clicking the Verify button for an SQL query with an inline variable substitution in Query Builder, for tools such as Make Feature Layer, Select, and Select Layer By Attribute, will give you an invalid SQL query error, as the verification does not substitute the value of the inline variable when evaluating an expression. When the tool runs, the value of the inline variable is substituted in the expression and the results are created based on the substituted values.

Example 4

This model contains two variables: Number of Residents and Waste Per Person Per Year. These variables are used in the Calculate Value tool expression by enclosing them in percent symbols. When the Calculate Value tool runs, the variable names will be substituted with their specified values and multiplied together to calculate total waste per year.

Use of inline variable in Calculate Value tool

Example 5

Miles to Meter is a variable in the model that contains a conversion factor for the number of meters in one mile (1,609.344). The input has a distance field with mile values. The Calculate Field tool uses the values from the distance field (in miles) to multiply with the conversion factor and generate the distance in meters. The expression of the Calculate Value tool is Miles * %Miles to Meter%. When the tool is run, %Miles to Meter% is replaced with 1609.344.

Use of inline variable in Calculate Field tool

Example 6

The example below shows the incorrect way to use inline variable substitution in a model and how the problem can be corrected.

The Input variable contains the value C:\ToolData\Road.shp. When used as an inline substitute in the output name as C:\Scratch\%Input%, the tool will give an error. This error occurs because the Input variable contains the path, name, and extension of the dataset, causing the substituted value to be C:\Scratch\C:\ToolData\Road.shp—an invalid path. To correctly handle this situation, use the Parse Path tool to separate the full path into its components and then use the dataset name (contained in the Name variable) for inline substitution.

How not to use inline variable

Related Topics

3/3/2014