In ModelBuilder or on the command line, you can use the contents of a variable as a substitute for another variable. The variables that you use for string substitution can come from
- Model variables
- Environment settings
- System variables (see below)
- Variables created at the command line
The model below adds a field to a table and calculates an initial value for the field. This model is meant to be run as a tool from ArcToolbox, which means that the user of the tool will provide the table, field name, and initial value. This raises an issue for the Calculate Field tool—it won't know the list of fields or the expression (initial value) until it executes, so you cannot determine these parameter values when designing the model tool.
Variable substitution solves this problem. In the Calculate Field tool, the Field Name parameter contains %Field Name%. At execution time, %Field Name% will be replaced with the value in the Field Name variable. Likewise for the Expression parameter - it will be replaced with the value in the Initial Value variable at execution time.
When using variable substitution
- Enclose the variable name in percent signs (%).
- Spaces within the variable name are allowed.
- Substitution is not case sensitive. "Initial Value" and "initial value" are equivalent.
Variable substitution is also useful for pathnames. In the example model below, the intermediate data is written to the pathname contained in the environment setting scratchWorkspace.
Learn how to obtain a list of environment settings to use in variable substitution
Learn more about environment settings
Models contain two built-in system variables, i
that are used in model iteration. Learn more about system variables in model iteration
Variable search order
When a model containing variable substitution executes, it looks for the variables in this order:
- Model variables
- Model environment settings
- System variables (i and n)
- Parent model—if the model is being run from another model, the parent model's variables and its environment variables are searched.
- Command line variables set with the Variable Manager
Learn more about command line variables