I have a channel field with no results
{if no_results} set variable here {/if}
I want to set a variable but use it further up the page
{if my_custom_var != "empty"} do some layout stuff here {/if}
If a variable is dependent on an entries loop, you definitely have to run that loop first to get the results. From there, it's up to you whether you set the variable with something like stash or raw PHP. A good PHP example is available on Stack Overflow. Just make sure you have your template's PHP Parsing Stage set to "Output".