# Define and Update {{ var }} to be used across different models

**URL:** <https://discourse.getdbt.com/t/define-and-update-var-to-be-used-across-different-models/20240>\
**Category:** Help\
**Tags:** dbt-cloud\
**Created:** [September 17, 2025, 3:17am UTC](https://discourse.getdbt.com/t/define-and-update-var-to-be-used-across-different-models/20240 "2025-09-17T03:17:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShashiK](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@ShashiK](https://discourse.getdbt.com/u/ShashiK)\
**Post date:** [September 17, 2025, 3:17am UTC](https://discourse.getdbt.com/t/define-and-update-var-to-be-used-across-different-models/20240/1 "2025-09-17T03:17:24Z")

</div>

## The problem I’m having

So I pass a variable to a model using this command

`dbt run –select model1 –vars ‘{`_`my_var`_`: None}’ `

In my model1, I want to update this global level variable _`my_var` using the output of a pre\_hook._

But I’m struggling to come up with the right syntax. I want to use this updated `my_var` across my models in job runs if i run models using `select tag:` syntax

## The context of why I’m trying to do this

I’m generating a unique ID, which is to be used in all my downstream models. So I want to generate this Unique Id once by calling the macro in model1.

and set the global variable with this unique id.

then all my remaining models (model2, model3, 4 etc) will get the updated value using {{ var }}.

## What I’ve already tried

inside my pre\_hook block of statements, i tried adding this after my macro

`"set var('my_var') = {{ this._pre_hook[0] }}",`

## Some example code or error messages

```sql
pre_hook = [
    "my_macro_which_generates_unique_id()",
    "set var('my_var') = {{ this._pre_hook[0] }}",
    "{{ log('[LOG]: ID value after pre-hook: ' ~ var('my_var'), info=True) }}",
]

```

The output doesnt show the updated id.  
`03:14:44 [LOG]: ID value after pre-hook: None`

---

<div class="post-metadata">

**Author:** ![a\_slack\_user](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/a_slack_user/32/1629_2.png) [@a\_slack\_user](https://discourse.getdbt.com/u/a_slack_user)\
**Post date:** [September 17, 2025, 4:10am UTC](https://discourse.getdbt.com/t/define-and-update-var-to-be-used-across-different-models/20240/2 "2025-09-17T04:10:29Z")

</div>

You cannot make a model set a “dbt var” and have other models read that var.

Note: `@Jeremy Yeo` originally [posted this reply in Slack](https://getdbt.slack.com/archives/CBSQTAPLG/p1758082220187949?thread_ts=1758079073.769379&cid=CBSQTAPLG). It might not have transferred perfectly.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/1X/a7a7ca1fe379aaf90952b0e13118a817babcd14f.png) [@system](https://discourse.getdbt.com/u/system)\
**Post date:** [October 6, 2025, 7:14pm UTC](https://discourse.getdbt.com/t/define-and-update-var-to-be-used-across-different-models/20240/3 "2025-10-06T19:14:19Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
