Help using Base SAS procedures

Why two consecutive semicolons are needed?

Reply
Frequent Contributor
Posts: 131

Why two consecutive semicolons are needed?

Hi sir,

I just run a macro as below. The macro is used to merge multiple datasets. The %else statement needs two consecutive semicolons ';' here. Otherwise, SAS reported that it cannot find the data. The log message shows that:

DATA work.model;

MPRINT(DOMERGE):   SET work.model work.data05 RUN;

So it does miss a semicolon here.

/*The macro program as below*/

%let num=16;

%macro domerge;

    %do i=1 %to #

      DATA work.model;

     %if &i=1 %then %do;

         SET work.data01 work.data02;

         %end;

     %else SET work.model work.data%sysfunc(putn(&i+1.,z2.));;

     RUN;

  %end;

%mend domerge;

%domerge

Respected Advisor
Posts: 3,799

Why two consecutive semicolons are needed?

The first one goes with the %ELSE the second one is emitted unconditionally and works as a null statement when &i eq 1 and when &i ne 1 it happens to provide the necessary semicolon for the SET emitted by %ELSE.

I usually almost always use %DO; code %end;  as you have with %IF.   I find it easier to understand as in this situation.

Frequent Contributor
Posts: 131

Why two consecutive semicolons are needed?

Posted in reply to data_null__

Hi data_null_,

Can I kow why there need to be two separate semicolons for %ELSE and SET, rather than a single semicolon for a statement.:smileyconfused:

Respected Advisor
Posts: 3,799

Re: Why two consecutive semicolons are needed?

Because  BOTH statements (%else and SET) need semicolons.

Alternatively;

%else %str(set ...; );

Contributor
Posts: 35

Why two consecutive semicolons are needed?

Remember that what a macro statement generates need not be a complete SAS statement. So, in the case of your %ELSE you need to do two completely distinct things - finish the %ELSE statement and finish the SET statement. With only a single semicolon SAS can't tell that your SAS statment is finished because the semicolon has already been used to finish the %ELSE.

Frequent Contributor
Posts: 101

Why two consecutive semicolons are needed?

data_null_ gave you the answer to your question. If it's too confusing you could write the %else with %do...%end as you did for the %if statement, such as:

%else %do:

     SET work.model work.data%sysfunc(putn(&i+1.,z2.));

%end;

That eliminates the consecutive semicolons and your confusion.

bncoxuk wrote:

Hi data_null_,

Can I kow why there need to be two separate semicolons for %ELSE and SET, rather than a single semicolon for a statement.

Ask a Question
Discussion stats
  • 5 replies
  • 1061 views
  • 0 likes
  • 4 in conversation