# Output variable only if defined

**URL:** <https://discourse.getdbt.com/t/output-variable-only-if-defined/11661>\
**Category:** Help\
**Tags:** jinja, dbt-core\
**Created:** [January 19, 2024, 8:59am UTC](https://discourse.getdbt.com/t/output-variable-only-if-defined/11661 "2024-01-19T08:59:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RoyalTS](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/royalts/32/3550_2.png) [@RoyalTS](https://discourse.getdbt.com/u/RoyalTS)\
**Post date:** [January 19, 2024, 8:59am UTC](https://discourse.getdbt.com/t/output-variable-only-if-defined/11661/1 "2024-01-19T08:59:54Z")

</div>

I’m trying to output a dbt variable into a jinja template conditional on that variable being defined. Alas, my naive attempt does not seem to work:

```auto
{% if var('my_variable') is defined %}WHERE f.source_date < '{{ var('my_variable') }}'::DATE{% endif %}

```

---

<div class="post-metadata">

**Author:** ![jeffsk](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/jeffsk/32/1250_2.png) [@jeffsk](https://discourse.getdbt.com/u/jeffsk)\
**Post date:** [January 19, 2024, 5:54pm UTC](https://discourse.getdbt.com/t/output-variable-only-if-defined/11661/2 "2024-01-19T17:54:38Z")

</div>

It is hard to know how you are defining it, under what conditions it will be defined, etc. But with the very little information provided, maybe this will help?

Just use Boolean instead. Start the model with:

`{% set my_variable = False %}`

 ![image](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/2X/9/90c85f75006900ae55ab51fd667fc5b68a9d32a3.png)

Then at some point in your script you flip it to true under certain circumstances?

 ![image](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/2X/8/8363368a82f385829f014bb25a38520f161a318a.png)

Try this website to play with it! [http://jinja.quantprogramming.com/](http://jinja.quantprogramming.com/)

Update:  
If you just get rid of “Is defined” it should work. Just make sure when you define it, you define it as “True”:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/2X/2/27f7067949324f5f4b2124022c658a409e91a5fd.png)

---

<div class="post-metadata">

**Author:** ![jeffsk](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/jeffsk/32/1250_2.png) [@jeffsk](https://discourse.getdbt.com/u/jeffsk)\
**Post date:** [January 19, 2024, 7:31pm UTC](https://discourse.getdbt.com/t/output-variable-only-if-defined/11661/3 "2024-01-19T19:31:40Z")

</div>

I was just re-reading this and realized your variable needs to be used in a date comparison. The same answer applies though:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/2X/a/af150ca6682e1fde646e2e4b087461181b7aac85.png)

vs

 ![image](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/2X/9/952a7210acbbb21ca2f4f67ee99859c08f20531a.png)

---

<div class="post-metadata">

**Author:** ![RoyalTS](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/royalts/32/3550_2.png) [@RoyalTS](https://discourse.getdbt.com/u/RoyalTS)\
**Post date:** [January 23, 2024, 2:25pm UTC](https://discourse.getdbt.com/t/output-variable-only-if-defined/11661/4 "2024-01-23T14:25:40Z")

</div>

Sorry, I should have been far clearer: The kinds of variables I want to be able to (optionally) use are the ones I pull via `var()`, not local jinja variables.

---

<div class="post-metadata">

**Author:** ![jeffsk](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/jeffsk/32/1250_2.png) [@jeffsk](https://discourse.getdbt.com/u/jeffsk)\
**Post date:** [March 24, 2024, 1:07pm UTC](https://discourse.getdbt.com/t/output-variable-only-if-defined/11661/5 "2024-03-24T13:07:11Z")

</div>

It works the same way but you just refer to it differently.
