In ModelBuilder, the value or dataset path of a variable can be substituted for another variable by enclosing the substituting variable name in percent signs (%VariableName%). Substituting variables in this manner is called inline variable substitution.
For example, if you have a variable Name with a value of Wilson, you can construct a dataset path as C:\Data\Output.gdb\Clipped_%Name% and the path will be resolved as C:\Data\Output.gdb\Clipped_Wilson.
You can perform inline variable substitution using any model variables or system variables.
- Model variables—Any variable in a model.
- System variable—ModelBuilder provides a built-in system variable, n, that can be used in iteration workflows. %n% is the iteration number in the model.
Model variable substitution
In the model illustrated 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%.
The use of model variable substitution is especially important when working with ModelBuilder iterators. When the iterator Iterate Feature Classes runs, it creates an output variable for both the path and name of each feature class in a workspace. The value in the Name variable can be used to construct the path to the output Projected Feature Class. When the tool executes, %Name% is replaced with the name of the feature class.
User input to a model tool
Model variable substitution can help you pass values entered by a user directly into a tool inside your model. For example, in the model illustrated below, Parcel ID is a model parameter that is specified when the model tool is run from the Geoprocessing pane. 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% in the expression is replaced with the parcel ID (9 in the case below), and only those parcels with an ID of 9 are selected.
Used with Calculate Value
Calculate Value is a powerful ModelBuilder utility that allows you to calculate a value based on any Python expression and use that value in your model. You can use model variable substitution to pass values from model variables into the Calculate Value Python expression, or use the output variable name that stores the calculated value in another tool in your model.
The model below contains two numeric 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 values and multiplied together to calculate Total Waste Per Year.
System variable substitution
ModelBuilder provides a built-in system variable that can be used in iteration workflows. This variable, %n%, refers to the current model iteration (the first iteration is zero) when a model contains an iterator.
For example, the For iterator is used to iterate a model four times. The output of the Buffer tool is used as feedback into the tool as input. The model iterates and creates a new output at each iteration. %n% is used in the output name of Buffer to give the output of each iteration a new name.