Sorry for the basic question but I am trying to figure out why data A has only 2 obs while data B has 4.
Expecting A to have 4 obs. what is the logic for having only 2 obs ?
/* A */
data a;
do i=1 to 4;
i+1;
output;
end;
run;
/* B */
%macro bb;
data b;
%do i=1 %to 4;
i+1;
output;
%end;
run;
%mend bb;
%bb;
Hello @Peter0123,
In the first DATA step, variable i is set to 1 by the DO statement, then incremented by 1 by the sum statement (i+1;). The resulting value 2 is written to the output dataset A by the OUTPUT statement. Then the DO loop iterates, increasing the value of i to 3, which in turn is incremented to 4 before the next execution of the OUTPUT statement. After the 4 has been written to A (as the second observation) the DO loop increments i to 5, beyond the specified end value 4, which terminates the loop and then the DATA step (as there is no subsequent statement between end; and run;).
The DATA step created by macro bb,
data b; i+1; output; i+1; output; i+1; output; i+1; output; run;
initializes variable i to 0 (as a side effect of the sum statement) and then increments and writes the incremented value to output dataset B four times, thus creating four observations with the values 1, 2, 3, 4.
Note that the "i" in "%do i=1 %to 4;" is a macro variable and has nothing to do with the DATA step variable i, which just happens to have the same name.
%DO and DO do not do the same things!
%DO cannot access the values in a DATA step variable such as data step variable I in your code, nor can it loop through the values of a DATA step variable. %DO works on the values of macro variables only.
Adding, while your data step is not wrong syntactically, as there are no ERRORs in the log, I wonder if it is doing what you want.
data a;
do i=1 to 4;
i+1;
output;
end;
run;
produces only 2 output records, with I having the values 2 and 4. If you want four records output, with values 1, 2, 3 and 4, then your line I+1 is incorrect here.
data a;
do i=1 to 4;
output;
end;
run;
In the first one you are incrementing the loop counter in the middle of the loop.
In the second one you are not. In fact their are NO macro statements inside the %DO loop. Only the SAS statements you are using the macro code to generate.
In the first one you ran this code:
do i=1 to 4;
i+1;
output;
end;
In the second one you ran this code:
i+1;
output;
i+1;
output;
i+1;
output;
i+1;
output;
To have them produce the same number of observations remove the sum statement from the first one so the loop will run 4 times.
But if you include a SET statement into the data step so that it iterates more than once they won't produce the same results since the first one will generate I values like: 1,2,3,4,1,2,3,4,... and the second one will generate I values like 1,2,3,4,5,6,7,8,... In that case the equivalent steps would be:
data new;
set old;
do i=1 to 4;
output;
end;
run;
%macro test;
data new;
set old;
%do i=1 %to 4;
i=&i;
output;
%end;
run;
%mend test;
%test;
Hello @Peter0123,
In the first DATA step, variable i is set to 1 by the DO statement, then incremented by 1 by the sum statement (i+1;). The resulting value 2 is written to the output dataset A by the OUTPUT statement. Then the DO loop iterates, increasing the value of i to 3, which in turn is incremented to 4 before the next execution of the OUTPUT statement. After the 4 has been written to A (as the second observation) the DO loop increments i to 5, beyond the specified end value 4, which terminates the loop and then the DATA step (as there is no subsequent statement between end; and run;).
The DATA step created by macro bb,
data b; i+1; output; i+1; output; i+1; output; i+1; output; run;
initializes variable i to 0 (as a side effect of the sum statement) and then increments and writes the incremented value to output dataset B four times, thus creating four observations with the values 1, 2, 3, 4.
Note that the "i" in "%do i=1 %to 4;" is a macro variable and has nothing to do with the DATA step variable i, which just happens to have the same name.
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.