07-12-2011 10:00 AM
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:
MPRINT(DOMERGE): SET work.model work.data05 RUN;
So it does miss a semicolon here.
/*The macro program as below*/
%do i=1 %to #
%if &i=1 %then %do;
SET work.data01 work.data02;
%else SET work.model work.data%sysfunc(putn(&i+1.,z2.));;
07-12-2011 10:09 AM
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.
07-12-2011 10:16 AM
Can I kow why there need to be two separate semicolons for %ELSE and SET, rather than a single semicolon for a statement.:smileyconfused:
07-12-2011 03:02 PM
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.
07-13-2011 09:37 PM
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:
SET work.model work.data%sysfunc(putn(&i+1.,z2.));
That eliminates the consecutive semicolons and your confusion.
Can I kow why there need to be two separate semicolons for %ELSE and SET, rather than a single semicolon for a statement.