BookmarkSubscribeRSS Feed
InêsMaximiano
Obsidian | Level 7

%MACRO validate;
%if &A = 0 %then %do; 
%createtable;
%end;
%else %do; 
%if &B = 0 %then %do; 
%createtable;
%end;
%else %do; 
%don't create table;
%end;
%end;
%Mend validate;

 

Does this code work?

%if &A = 0 then &B = 0, so always create table

But it %if &A <> 0, &B can be 0 (and we want it to create table) or it can be <> 0 and we don't want it to create table. 

Because &A = &B + &C

 

Am I missing any %end or %else do?

4 REPLIES 4
A_Kh
Barite | Level 11

It seems to me the problem was overcomplicated. If &A= &B+&C and you need the table only when &A=0, then there is no need for &B= condition. 
I would simplify it like: 

%MACRO validate;
	%if &A = 0 %then %do; 
		%createtable;
	%end;
	%else %do;
		%put NOTE: Condition is not true, table is not created; 
		%return;
	%end;
%Mend validate;



Astounding
PROC Star

Definitely a problem.  While unbalanced single quotes can create problems, this is a call to the macro DON

%don

The simplest version of this macro might be:

%if &A=0 or &B=0 %then %createtable;
Tom
Super User Tom
Super User

This line is a problem. 

%don't create table;

Hopefully that is not in your actual code because it could cause your macro to emit unbalance single quotes which will "eat" a lot of code until it finds a closing quote.

 

You can test both A and B.

%if (&A=0) or (&B=0) %then %do;
  * code to create table ;
%end;

Or perhaps test B and C?

%if (&B=0) or (&B=-&C) %then %do;
  * code to create table ;
%end;

And if A or B might not be integers (1.5 for instance) then use %SYSEVALF().

%if %sysevalf( (&A=0) or (&B=0) ) %then %do;
  * code to create table ;
%end;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1029 views
  • 2 likes
  • 5 in conversation