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
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.
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:
Because BOTH statements (%else and SET) need semicolons.
Alternatively;
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.