BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shawn123
Obsidian | Level 7
%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?

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

 

 

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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.

 

 

--
Paige Miller
shawn123
Obsidian | Level 7
Thank you so much, so If Marco's statement and data step statement are writing together I need to treat them as separate statements?
PaigeMiller
Diamond | Level 26

@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)

 

--
Paige Miller
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------