BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Peter0123
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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.

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

%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.

--
Paige Miller
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
Tom
Super User Tom
Super User

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;
FreelanceReinh
Jade | Level 19

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.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 538 views
  • 7 likes
  • 4 in conversation