BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cau83
Pyrite | Level 9

%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;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

8 REPLIES 8
Astounding
PROC Star

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.

cau83
Pyrite | Level 9

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...

Tom
Super User Tom
Super User

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.

cau83
Pyrite | Level 9

"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?

Tom
Super User Tom
Super User

I use it when doing macro variable "arrays".

%let name1=Fred ;

%let name2=Sam;

%let i=2;

%put &&name&i ;

Astounding
PROC Star

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.

cau83
Pyrite | Level 9

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.

Tom
Super User Tom
Super User

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 ;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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