DATA Step, Macro, Functions and more

Combining explicit and implicit loop

Accepted Solution Solved
Reply
Super Contributor
Posts: 285
Accepted Solution

Combining explicit and implicit loop

[ Edited ]

Hello,

How does this program execute?


data test;
	set sashelp.class;

	do until(eof);
		set sashelp.class end=eof;
	 output;
	end;
run;

Accepted Solutions
Solution
‎10-04-2017 04:58 PM
Super User
Posts: 13,941

Re: Combining explicit and implicit loop

Posted in reply to SAS_inquisitive

Look at the log after running this and see if you get any enlightenment:

data test;
	set sashelp.class;
   put 'Before loop';
	do until(eof);
      put 'At begin of loop' +1 name=;
		set sashelp.class end=eof;
      put 'In loop after set' +1 name=;
	 output;
      put;
	end;
run;

View solution in original post


All Replies
Super User
Posts: 10,574

Re: Combining explicit and implicit loop

Posted in reply to SAS_inquisitive

Try it, and read the log. If puzzled, come back here.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Trusted Advisor
Posts: 1,394

Re: Combining explicit and implicit loop

Posted in reply to SAS_inquisitive

Form a hypothesis test it, report the results, and why you think those results occurred.  Then you'll get plenty of responses.

Super Contributor
Posts: 285

Re: Combining explicit and implicit loop

@KurtBremser and @mkeintz I was wondering why two records are reading from first set statement?
Trusted Advisor
Posts: 1,394

Re: Combining explicit and implicit loop

Posted in reply to SAS_inquisitive
  1. How many times do you think the complete loop    "do until (eof); ....... " is successfully run?

  2. What happens when a SET statement attempts to read beyond end of data?
Super Contributor
Posts: 285

Re: Combining explicit and implicit loop

[ Edited ]

@mkeintz  1.  "do until (eof); ....... "  runs just in one loop as it is DOW-LOOP setting.

                   2.  _n_ increments by 1 and implicit loop terminates.


data test;
    put _all_;
	set sashelp.class;
	put _all_;
run;

data test;
	
	put _all_;
	do until(eof);
      
		set sashelp.class end=eof;
		output;
    put _all_;
	end;
	
run;

  

Solution
‎10-04-2017 04:58 PM
Super User
Posts: 13,941

Re: Combining explicit and implicit loop

Posted in reply to SAS_inquisitive

Look at the log after running this and see if you get any enlightenment:

data test;
	set sashelp.class;
   put 'Before loop';
	do until(eof);
      put 'At begin of loop' +1 name=;
		set sashelp.class end=eof;
      put 'In loop after set' +1 name=;
	 output;
      put;
	end;
run;
SAS Super FREQ
Posts: 4,274

Re: Combining explicit and implicit loop

Building on ballardw's advice, try

data test;
	set sashelp.class(rename=(Name=FirstName));
   put 'Before loop';
	do until(eof);
      put 'At begin of loop' +1 FirstName=;
		set sashelp.class end=eof;
      put 'In loop after set' +1 FirstName= +1 name=;
	 output;
      put;
	end;
run;
Super Contributor
Posts: 285

Re: Combining explicit and implicit loop

@Rick_SAS, renaming helped understand since the original solution was given by @Ballard, I accepted his solution. Thanks .
Super User
Posts: 10,574

Re: Combining explicit and implicit loop

Posted in reply to SAS_inquisitive

A nice variation to further illustrate the behaviour of set statements and loops:

data test;
  set sashelp.class;
  do while (not eof);
    set sashelp.class end=eof;
    output;
  end;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Esteemed Advisor
Posts: 5,625

Re: Combining explicit and implicit loop

Posted in reply to SAS_inquisitive

The question you may want to ask instead is When does this program stop?

PG
Super Contributor
Posts: 285

Re: Combining explicit and implicit loop

@PGStats, you are absolutely right.
☑ This topic is solved.

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

Discussion stats
  • 11 replies
  • 403 views
  • 8 likes
  • 6 in conversation