Working with variables


Be sure to read Model elements for a complete definition and discussion of variables.

You can think of a variable as a container that holds a value that can be changed. In the context of a model, a variable can be created and its value used as a tool's parameter value.

There are two basic kinds of variables: data variables and value variables.

Variables

Data variables are discussed in Creating model processes, which shows you how to create data variables and connect them to tool parameters.

Value variables are things such as strings, numbers, Booleans (true/false values), spatial references, linear units, extents, and so forth. In short, value variables contain anything but references to data on disk.

There are only minor differences between data and value variables.

Value variables are connected to tool parameters in the same way you connect data variables.

This section focuses on how to create and manage variables in general, so it applies to both data and value variables.

Why create variables?

Making variables from parameters

You can make a variable from any tool parameter. Right-click the tool and point to Make Variable > From Parameter, then click the parameter, as shown below.

Making variable from parameter

Variables made in this way are automatically connected to the tool.

Creating stand-alone variables

Another way to create a data or value variable is to right-click the model diagram and click Create Variable. The following dialog box will appear where you choose the data type of the variable you want to create.

Learn more about variable data types

Creating a stand alone variable

After choosing a data type for your variable and clicking OK, a new empty variable will be created. You can then connect the variable to one or more tools, set a value for the variable, and/or make it a model parameter.

The Multivalue option will create a variable that allows multiple values to be input, as illustrated below.

Multivalue control

Learn more about using the multivalue parameter control

Learn more about using a multivalue in script tools

Variables and data types

Every parameter and variable has an associated data type. For example, the first parameter for the Dissolve tool is <in_features>, and its data type is feature layer.

Learn more about geoprocessing data types

Changing a data type

You can right-click a variable, click Properties, and change the data type of the variable in the Data Type tab. The list of data types you can choose are those that support the current data type. The example that follows shows how changing a data type can be useful.

The example model below shows the Create Random Points tool with the Constraining Extent input variable as a model parameter. This variable is an Extent data type, which is basically four numbers that define a geographic rectangle. Consider that these four numbers can be retrieved from any feature class, feature dataset, layer, TIN, network, and so on. It can be said that these latter data types (feature class, feature dataset, and so on) support the Extent data type, because a geographic extent can be retrieved from them.

Example model

To change the Constraining Extent data type, right-click the variable and click properties. In the Data Type tab, select a new data type, as shown below. Only those data types that support the current data type (in this case, Extent) will be shown. For example, the Table data type does not support Extent, show it will not show in the list.

Changing a data type

After changing the data type to Feature Class, for example, and opening the model's tool dialog from ArcToolbox, you'd enter a feature class for the Constraining Extent, rather then four numbers.

Setting variable values

To set the value of a variable, double-click the variable or right-click and click Open. This will open the user interface control and allow you to set the value. You cannot set the value of an in/out derived variable using this method. For in/out derived data variables, you must change the value of the input data variable.

Learn more about in/out derived variables

NOTE: Once a variable element is connected to a tool parameter, you cannot set the value of the variable using the tool dialog, since the parameter control shows the name of the associated variable, not its contents. If you enter a new value using the tool's parameter control, a new variable will be created and connected, and the old variable will be disconnected.

Connecting variables to tools

There are two ways you can connect variables to tool parameters, either by using the Connect tool Connect tool or the tool's dialog.

To use the Connection Tool:

  1. Click the Connect tool.
  2. Click the variable you wish to connect to a tool.
  3. Click the tool to which you want to connect the variable.

A connection will be made between the variable and the tool.

To use the tool's dialog, right-click the tool and click Open. Typically, the parameter controls in the tool will allow you to pick model variables. However, this is not always the case. Consider the following simple model containing the Add Field tool. There are three stand-alone variables that need to be connected to the Add Field tool: Customers, New Field Name, and Field can contain nulls. Customers is a data variable created using the Add Data tool Add data tool.

Model with value variables

Using the tool dialog, you can connect Customers to the input table parameter by exposing the drop-down list, as illustrated below. Data variables are shown with the Data variable icon icon. The illustration also shows available layers from the ArcMap table of contents in the drop-down list with the Layer icon icon. Once you pick Customers and click OK, a connection between Customers and the Add Field tool will be created.

Data variable in drop-down list

For the Field Name parameter, you can choose the variable named New Field Name from the drop-down list. Since the variable New Field Name is a value variable, not a data variable, it will not have the Data variable icon icon.

String variable in drop-down

Finally, the Add Field tool contains two Boolean parameters, Field IsNullable and Field IsRequired. However, you cannot use the parameter control to connect the variable named New Field Name—you must use the Connection tool. There are other parameter controls, such as Linear Unit, that behave the same way—you must use the Connect tool rather than the tool dialog.

Boolean variable

Displaying valid parameters when connecting variables

By default, when you use the Connect tool to connect a variable to a tool, the variable is assumed to be that of the first parameter whose data type matches the variable. For example, the Add Field tool takes two Boolean variables, Field IsNullable and Field IsRequired. If you connect a Boolean variable to the Add Field tool, which of these two parameters will it connect to?

To specify which parameter the variable will be connected to, you need to instruct ModelBuilder to display a list of valid parameters when connecting data to tools, as follows:

  1. On the Tools menu of any ArcGIS application, such as ArcMap or ArcCatalog, click Options.
  2. Click the Geoprocessing tab.
  3. Check the display valid parameters option, as shown below.
  4. Display valid parameters option

There's no need to exit ModelBuilder to check this option—you can check it while you're working in ModelBuilder.

After checking this option, anytime you connect a variable to a tool, you'll get a message box asking you which parameter you want to connect to. Here is an example of connecting a Boolean variable to the Add Field tool:

Select parameter

Creating variables for environment settings

Variables can be created for environment settings. Right-click the tool and point to Make Variable > From Environment. You can also create a stand-alone variable and associate it with a model's environment setting.

Learn more about model environments

See Also