%if ... %then ... %do ... %end;

Accepted Solution Solved
Reply
Contributor fre
Contributor
Posts: 33
Accepted Solution

%if ... %then ... %do ... %end;

Hi,

I just wanted to ask if this is normal behaviour:

When I use following statements in a macro, it always gives an error when running:

....

%if &pdcty = 'A' %then CD_PERIOD = 0;

....

the error is: ERROR 22-322: syntax error, expecting one of the following: !, !!, etc....

When I transform the code to:

....

%if &pdcty = 'A' %then %do;

CD_PERIOD = 0;

%end;

....

no errors are given.  Is this normal?  I thougt the %do ... %end statements are only necessary when you have more than one command to do when the if-condition is true.


Accepted Solutions
Solution
‎10-21-2013 10:27 AM
Super User
Posts: 5,072

Re: %if ... %then ... %do ... %end;

It may help to understand the reason why.  Using the %DO group, you may be adding this code to your program:

CD_PERIOD = 0;

Using a single %IF %THEN statement, you may be adding this code to your program:

CD_PERIOD = 0

The semicolon at the end of the %IF THEN statement is just ending the %IF %THEN statement.  It is not part of the generated SAS code.

View solution in original post


All Replies
Super User
Posts: 5,255

Re: %if ... %then ... %do ... %end;

This is partly true, but %do is not needed only if you are referring to macro statements, such as %let. Any non-macro code that needs to be "output" needs to be enclosed in %do-%end blocks.

Data never sleeps
Respected Advisor
Posts: 3,777

Re: %if ... %then ... %do ... %end;

You just need an extra semicolor for the first one to work.  I like %do; %end; even when the first construct would be OK,  I think it is easier to read.

Solution
‎10-21-2013 10:27 AM
Super User
Posts: 5,072

Re: %if ... %then ... %do ... %end;

It may help to understand the reason why.  Using the %DO group, you may be adding this code to your program:

CD_PERIOD = 0;

Using a single %IF %THEN statement, you may be adding this code to your program:

CD_PERIOD = 0

The semicolon at the end of the %IF THEN statement is just ending the %IF %THEN statement.  It is not part of the generated SAS code.

Contributor fre
Contributor
Posts: 33

Re: %if ... %then ... %do ... %end;

thanks, it's very clear to me now;

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 264 views
  • 6 likes
  • 4 in conversation