Search code examples
sassas-macro

SAS using global variables in conditional comparisons of characters


I have an if statement that checks whether a global variable is yes/no

The global variable is the output from a previous macro

Why is it when I use quotes around the 'Y' or 'N' it doesn't work - i've never seen this in any other progamming language. I've always had to use quotes for character variables

i.e. this does not work

%if &errorflag='Y' %then %do;

but this works:

%if &errorflag=Y %then %do;

Solution

  • Macros are nothing but a text substitution engine. So the value contained in &errorflag is Y and not 'Y'. Obviously Y ^= 'Y'.

    This would work

    %if "&errorflag" = "Y" %then %do;
    

    It surrounds the value in &errorflag in quotes.

    %if "%upcase(&errorflag)" = "Y" %then %do;
    

    Might be a safer comparison.