DATA Step, Macro, Functions and more

Want to check if macro variable = 0 as part of %if condition

Accepted Solution Solved
Reply
Contributor
Posts: 69
Accepted Solution

Want to check if macro variable = 0 as part of %if condition

%ifSample code is below. It is a simplification-- I am calling the macro CheckData as part of a bigger macro. I want to check the data set &gdatasrc._forecast to see if the maximum of a variable is zero-- if so, I want to avoid running a report later on in the project. I also want to be able to email myself a report that tells me what types of anomalies were encountered (The CheckDataMsg1 macro).

The SQL statement works and I get the macro variable to equal to 0 but there are leading and trailing blanks. I've tried using %STR, %EVAL, and other functions with which I'm familiar (though haven't used much) to try and get SAS to evaluate that %IF as TRUE but it's always turning out false. I want to use the same if condition to control whether or not I run and distribute the reports later in the program that I referenced later so it will be a crucial component (and the reason for needing it to be in the global symbol table).

What am I doing wrong/what do I need to add to get the %if to evaluate as true?

%macro Bigger-Macro(gdatasrc);

%macro CheckData;

%global &gdatasrc._maxeffort;

proc sql;

    select max(effort) into :&gdatasrc._maxeffort from &gdatasrc._forecast;

quit;

%if  &&gdatasrc._maxeffort = 0 %then %do;

  %global CheckDataMsg1=Most Recent Date has all zeroes for effort (&gdatasrc.);

%end;

%mend;

%mend;


Accepted Solutions
Solution
‎03-18-2014 05:40 PM
Super User
Posts: 5,071

Re: Want to check if macro variable = 0 as part of %if condition

Leading and trailing blanks won't make a difference in your comparison.  The problem is that you need another ampersand:

%if &&&gdatasrc._maxeffort=0 %then %do;

Good luck.

View solution in original post


All Replies
Solution
‎03-18-2014 05:40 PM
Super User
Posts: 5,071

Re: Want to check if macro variable = 0 as part of %if condition

Leading and trailing blanks won't make a difference in your comparison.  The problem is that you need another ampersand:

%if &&&gdatasrc._maxeffort=0 %then %do;

Good luck.

Contributor
Posts: 69

Re: Want to check if macro variable = 0 as part of %if condition

Thanks.

Now, can I ask why that is the case? I knew (thought I knew) I needed 2 ampersands but what is the third one doing?Looking back I can see that it was not fully resolving the variable in the way I needed but I do not understand why...

Super User
Super User
Posts: 6,495

Re: Want to check if macro variable = 0 as part of %if condition

So say that

%let gdatasrc = XXX ;

Then when SAS evaluates &&&gdatasrc._maxeffort it will make two passes.  After the first it will have convert the first two & into one and used the third to resolve &gdatasrc. to XXX.  So for the second pass it is looking at &XXX_maxeffort which is the variable that you set in the SQL code.

With only two & the first pass just converts the two into one and you end of with the same as if you had only used one & to begin with.

Note you should probably add a %LET before the proc sql call to set the value that you want the new global macro variable to have when there are no records in the input data set.

Contributor
Posts: 69

Re: Want to check if macro variable = 0 as part of %if condition

"With only two & the first pass just converts the two into one and you end of with the same as if you had only used one & to begin with."

Thanks Tom, it's making more sense now. But now I'm wondering when would one ever use only 2 ampersand's?

Super User
Super User
Posts: 6,495

Re: Want to check if macro variable = 0 as part of %if condition

I use it when doing macro variable "arrays".

%let name1=Fred ;

%let name2=Sam;

%let i=2;

%put &&name&i ;

Super User
Posts: 5,071

Re: Want to check if macro variable = 0 as part of %if condition

Chris,

Two ampersands are often used when you need to delay resolution until a string on the right can resolve.  It's easier to take an example rather than figure out what that means.  Let's say you have 10 macro variables named v1 - v10 that you would like to process.  (To illustrate, all we'll do is print their values.)  You might use code like this:

%do i=1 %to 10;

   %put &&v&i;

%end;

When &i is 1, after the first pass through this resolves into &v1.  After the second pass through, it resolves into the value of the macro variable.

Contributor
Posts: 69

Re: Want to check if macro variable = 0 as part of %if condition

Ahh this makes sense, in fact reminds me that I have done that before (which is why it nagged me). I have a macro loop that goes through and adds the counter to the end of a data set name and that entire string is preceded by 2 ampersands.

Super User
Super User
Posts: 6,495

Re: Want to check if macro variable = 0 as part of %if condition

If you had made the beginning of the macro variable name the constant instead of the ending then you could have used && in your example.

%let gdatasrc=xxx ;

%let maxeffort_&gdatasrc = 101 ;

%put &&maxeffort_&gdatasrc ;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 614 views
  • 3 likes
  • 3 in conversation