Tuesday, December 13, 2011

Using double semicolon with macro statement in data step

Using macro statement within data step.

Each macro statement ends with a semicolon. If the macro statement involved generating new data step statement, it is important to distinguish the semicolon that is part of the data step statement and the semicolon that is part of the macro statement. When data step statement is nested inside a macro statement, we need to use double semicolons to end the whole macro part. e.g.
data a;
set a;
%if &group= %then _group=1;
%else rename &group=_group;;
run;
The macro in the data step will resolve to either ‘_group=1’ or ‘rename &group=_group’, depending on the value of &group. Note the resolved statement does not have semicolon by itself. Now, note the double semicolon at the end of the %if %else macro. The first semicolon (red) represents the end of the %if %else macro; then second semicolon (yellow) represents the semicolon that is carried over for the resolved data step statement (either _group=1 or rename &group=_group). Now the resolved data step statement is ended with a semicolon.