%macro output;
%do i=1 %to 10;
data output&i;
%if &i lt 5 %then assignment="&i";; /*??????*/
run;
%end;
%mend;
%output;
why I need double semicolon? what condition do I need double semicolon?
The %IF needs a semicolon and the data step command needs a semicolon.
The first semicolon ends the %IF statement.
The second semicolon ends the assignment="&i" statement in the data step.
The %IF needs a semicolon and the data step command needs a semicolon.
The first semicolon ends the %IF statement.
The second semicolon ends the assignment="&i" statement in the data step.
@shawn123 wrote:
Thank you so much, so If Marco's statement and data step statement are writing together I need to treat them as separate statements?
It would help if I explained briefly how macro statements work.
When you write code with macro statements, and then run the code, the macro statement is resolved and replaced by non-macro code, which SAS then executes. So the part of your code (with one semicolon)
%if &i lt 5 %then assignment="&i";
is replaced by assignment="1" when &i=1 and this is not a complete SAS data step command, you have to have a semicolon at the end to complete the command.
A slightly superior way to write this is:
%if &i lt 5 %then assignment="&i"%str(;);
where the
%str(;)
resolves to a semicolon and this is the semicolon that ends the assignment="1" statement; and will only appear when &i lt 5.
So when &i is less than 5, the macro statement resolves to assignment="1"; (with a semicolon on the end)
When &i is greater than or equal to 5, the macro statement does not create any SAS code. (If you did it the original way you wrote the program, when &i is greater than or equal to 5, a semicolon is produced ... essentially a blank statement ending with a semicolon, and while in this simple example it makes no difference, there are other cases where it might make a difference)
If you reconstruct
%if &i lt 5 %then assignment="&i";;
as
%if &i lt 5 %then %str(assignment="&i";);
it might be a little more evident.
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!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.