BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

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;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

1 REPLY 1
ballardw
Super User

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.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 1 reply
  • 962 views
  • 0 likes
  • 2 in conversation