# call statement inside macros returns None

**URL:** <https://discourse.getdbt.com/t/call-statement-inside-macros-returns-none/16350>\
**Category:** Help\
**Tags:** jinja, macros, dbt-core\
**Created:** [September 30, 2024, 3:44pm UTC](https://discourse.getdbt.com/t/call-statement-inside-macros-returns-none/16350 "2024-09-30T15:44:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giorgi\_Kratsashvili](https://avatars.discourse-cdn.com/v4/letter/g/46a35a/32.png) [@Giorgi\_Kratsashvili](https://discourse.getdbt.com/u/Giorgi_Kratsashvili)\
**Post date:** [September 30, 2024, 3:44pm UTC](https://discourse.getdbt.com/t/call-statement-inside-macros-returns-none/16350/1 "2024-09-30T15:44:34Z")

</div>

## call statement inside macros returns None

## Im trying to set variable using sql query (also using other set variables from before), it works fine outside of macros, but as soon as i put it in macros, the call statement returns None, so when i try to get result[‘Data’][0][0], I get an error of ‘None’ has no attribute ‘data’

- Ive tried passing datekey in macros manually by setting it inside as variable
- ive trying executing same querry manyally and executing same logic outside of macros and it works fine
- ive tried logging and checking if previous variables i use inside of query are being set as they are supposed to be and they are all fine

## the code inside macros

```auto
{% macro mcr_GetUserAmountByFreezingDate(datekey) %}

    {%set CalculationDate = mcr_DateKey_to_Date(datekey)%}
    {%set StartDateKey = mcr_Start_of_Month_Datekey(datekey)%}

    {%- call statement('my_statement', fetch_result=True) -%}
        SELECT MAX(FreezingDate) as max_date
        FROM {{source ('dwp_input','FreezingDate')}} (NOLOCK)
        WHERE FreezingEndOfMonth <= EOMONTH({{CalculationDate}})
    {%- endcall -%}

    {%- set max_date = "'" ~ load_result('my_statement')['data'][0][0] ~ "'" -%}
    {%set FreezingDateKey = mcr_DateTime_to_DateKey(max_date)%}
    SELECT {{FreezingDateKey }} as frz_datekey

{% endmacro %}

```

## the code to call macro

```auto
{{mcr_GetUserAmountByFreezingDate('20240930')}}

```

## the code that works outside macro

```auto
{%set datekey = 20240930%}

{%set CalculationDate = mcr_DateKey_to_Date(datekey)%}
{%set StartDateKey = mcr_Start_of_Month_Datekey(datekey)%}

{%- call statement('my_statement', fetch_result=True) -%}
    SELECT MAX(FreezingDate) as max_date
    FROM {{source ('dwp_input','FreezingDate')}} (NOLOCK)
    WHERE FreezingEndOfMonth <= EOMONTH({{CalculationDate}})
{%- endcall -%}

{%- set max_date = "'" ~ load_result('my_statement')['data'][0][0] ~ "'" -%}
{%set FreezingDateKey = mcr_DateTime_to_DateKey(max_date)%}

SELECT {{FreezingDateKey}} as frz_max

```

## This last code snippet returns the result just fine, if anyone could help me solve this error i would greatly appreciate it, thanks in advance!

---

<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 30, 2024, 3:58pm UTC](https://discourse.getdbt.com/t/call-statement-inside-macros-returns-none/16350/2 "2024-09-30T15:58:50Z")

</div>

Just verifying that both the source table is named ‘FreezingDate’ and the column you want the max of is named ‘FreezingDate’?

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

---

<div class="post-metadata">

**Author:** ![Giorgi\_Kratsashvili](https://avatars.discourse-cdn.com/v4/letter/g/46a35a/32.png) [@Giorgi\_Kratsashvili](https://discourse.getdbt.com/u/Giorgi_Kratsashvili)\
**Post date:** [September 30, 2024, 4:02pm UTC](https://discourse.getdbt.com/t/call-statement-inside-macros-returns-none/16350/3 "2024-09-30T16:02:00Z")

</div>

Yes, i just found the problem and came here to post, so the issue is fixed by placing the logic of macros inside an if statement to check if the macros is in the execute state,  
{% if execute %}  
code  
{% endif %}

…like this
