1
votes
%macro pp();
data temp1;
 set dir.data
 call symput('prod', product); 
run;
%put "&prod";
%if prod = "&prod" %then %do;
   %put "&prod";
%end;
%mend;
%pp();

Why does the if statement evaluate to false?

2

2 Answers

4
votes

In the SAS macro language, everything is a character string, so your statement

%if prod = "&prod" %then %do;

Will never be true; the string prod will never equal the string "&prod" if only because one string includes double-quotes and the other does not.

So use double-quotes on both sides or not at all. Either of these will be better:

%if "prod" = "&prod" %then %do;
%if  prod  =  &prod  %then %do;

Also, note that after this repair, the statement will be "true' only if the macro variable you created has the exact value of prod (those four characters). Case matters: prod is not equal to PROD.

1
votes

The best way to debug something like this is to put it in a %put statement which I can see you have tried to do but it can get a little tricky. Because your comparison statement is:

%if prod = "&prod" %then %do;

Then to debug it with a %put you should have included the full comparison (both sides) to make it stand out more:

%put prod = "&prod";

The output from this would show you that the string to the left of the equals sign does not equate to the string to the right. Part of the problem is that you are quoting the string on the right, but not on the left. Even if your macro variable &prod contained the value prod you are basically testing this condition:

prod = "prod"

A better comparison would be to wrap both strings in quotes like so:

%if "prod" = "&prod" %then %do;

In fact in the macro language a double quote is almost-similar to any other character. So we could have wrapped them in characters other than a double quotes:

%if ###prod@@ = ###&prod@@ %then %do;

The important thing is to treat them the same. You could even omit the double quotes although I think this sometimes leads to problems because it makes it hard to debug if the string contains macro quoted blank spaces or non-printable chars:

%if prod = &prod %then %do;  /* LEAST FAVOURITE OPTION AS IT CAN BE HARD TO DEBUG */

Hope this helps.