In-line variable substitution

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

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.

Model using variable substitution

When using variable substitution

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.

Variable substitution for pathname

Learn how to obtain a list of environment settings to use in variable substitution

Learn more about environment settings

System variables

Models contain two built-in system variables, i and n 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:

  1. Model variables
  2. Model environment settings
  3. System variables (i and n)
  4. Parent model—if the model is being run from another model, the parent model's variables and its environment variables are searched.
  5. Command line variables set with the Variable Manager
  6. Learn more about command line variables

See Also