BookmarkSubscribeRSS Feed
bncoxuk
Obsidian | Level 7

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

5 REPLIES 5
data_null__
Jade | Level 19

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.

bncoxuk
Obsidian | Level 7

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:

data_null__
Jade | Level 19

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

Alternatively;

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

DouglasMartin
Calcite | Level 5

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.

FloydNevseta
Pyrite | Level 9

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.

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 5736 views
  • 0 likes
  • 4 in conversation