DATA Step, Macro, Functions and more

statement between inner and outer do loop in nested do loop

Accepted Solution Solved
Reply
Regular Contributor
Posts: 234
Accepted Solution

statement between inner and outer do loop in nested do loop

How does the statement between inner and outer do loop execute? Under which conditions is it necessary to put statement  between inner and outer loop?

 

data test1;
	put _all_;

	do i= 1 to 2;
		a=10;

		do j=1 to 3;
			put _all_;
			output;
		end;
	end;
run;

data test2;
	put _all_;

	do i= 1 to 2;
		do j=1 to 3;
			a=10;
			put _all_;
			output;
		end;
	end;
run;

Accepted Solutions
Solution
‎03-15-2016 05:27 PM
Super User
Posts: 10,497

Re: statement between inner and outer do loop in nested do loop

The difference would be when to you want to change a value or perform an action.

If you want to execute it before the J loop, then the first. If you want to change the value or do the action each time then inside the inner loop. Your example doesn't really show much difference. But look at this difference:

data test1;
	put _all_;
        a=0;
	do i= 1 to 2;
		a=  a + 1;

		do j=1 to 3;
			put _all_;
			output;
		end;
	end;
run;

data test2;
	put _all_;
        a=0;
	do i= 1 to 2;
		do j=1 to 3;
			a= a + 1;
			put _all_;
			output;
		end;
	end;
run;

You may want to do something only after the the inner loop has completed, in which case it would go after the inner loop.

 

View solution in original post


All Replies
Solution
‎03-15-2016 05:27 PM
Super User
Posts: 10,497

Re: statement between inner and outer do loop in nested do loop

The difference would be when to you want to change a value or perform an action.

If you want to execute it before the J loop, then the first. If you want to change the value or do the action each time then inside the inner loop. Your example doesn't really show much difference. But look at this difference:

data test1;
	put _all_;
        a=0;
	do i= 1 to 2;
		a=  a + 1;

		do j=1 to 3;
			put _all_;
			output;
		end;
	end;
run;

data test2;
	put _all_;
        a=0;
	do i= 1 to 2;
		do j=1 to 3;
			a= a + 1;
			put _all_;
			output;
		end;
	end;
run;

You may want to do something only after the the inner loop has completed, in which case it would go after the inner loop.

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 282 views
  • 0 likes
  • 2 in conversation