How many times does the data step iterate when using set statement or input statement inside a do loop?

Frequent Contributor
Posts: 103

How many times does the data step iterate when using set statement or input statement inside a do loop?

How many times does the data step iterate when using set  statement or input statement inside a do loop. For example

Example1:

data learn;

do i=1 to 10;

set have;

output;

end;

run;

I understand when index variable i=1 when sas reads of the first obs in have, output statment writes to output dataset, then after end statement, it goes back to the top of the loop and I believe reads the 2nd observation and so on. Now, my question is once that comes outside the loop, as it hits the run statement, step boundary is reached. My understanding, is the loop iterated 1o times and the datatep from data to run iterates only once. Is my understanding correct? Please explain and correct my understanding!

Example2:

data learn;

do i=1 to 10;

set have;

end;/*without the explicit output statement*/

run;

here again, the loop iterates 10 times and the implicit output writes the last obs from have to the output dataset, but the datastep from data to run iterates only once. Is my understanding correct? Please explain and correct my understanding!

Example3:

data learn;

do until (last);

set have;

end;

run;

is this exactly the same as

data learn;

do i=1 to nobs;

set have nobs=nobs;

end;

run;

also, my understanding is that the datastep iterates only once.  Is my understanding correct? Please explain and correct my understanding!

Example4:

data learn;

do until(last.id)

set have;

by id;

/*some sas statments*/;

end;

run;  How many times the loop iterate and how many times does the datastep iterate here? also what happens when using explicit output and not explicit output here?

Posts: 3,852

Re: How many times does the data step iterate when using set statement or input statement inside a do loop?

First you have to define HAVE.  And then test test test.

data learn;
put 'NOTE: ' _n_ =;
do i=1 to 10;

set sashelp.class;
output;

end;

run;
NOTE: _N_=
1
NOTE: _N_=
2
NOTE: There were
19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.LEARN has
19 observations and 6 variables.
Contributor
Posts: 50

Re: How many times does the data step iterate when using set statement or input statement inside a do loop?

data_null,

Isn't it example of dow loop?

Posts: 3,852

DOW?

Contributor
Posts: 50

Re: How many times does the data step iterate when using set statement or input statement inside a do loop?

data_null,

I was saying if the example you gave is  DOW loop.

http://support.sas.com/resources/papers/proceedings12/052-2012.pdf

Posts: 3,852

Re: How many times does the data step iterate when using set statement or input statement inside a do loop?

Oh! Yes I suppose so.  Although usually DOW has UNTIL(LAST.<byvar>);

As far as I know this SUGI presentation "The SAS Supervisor" by is the first reference I have found to using DO UNTIL(LAST.<BYVAR>);

I was there!

I don't have a SAS82 user guide anymore but there may be example in that document.  Does anyone still have (ever had) one of those?

Regular Contributor
Posts: 167

Re: How many times does the data step iterate when using set statement or input statement inside a do loop?

I just dug out my SAS82 manual and I did not see an example of that in there.

As far as I know, the supervisor paper that data_null_; mentioned is the first reference to that construct which was later popularized on SAS-L by Ian Whitlock.

Super User
Posts: 6,764

Re: How many times does the data step iterate when using set statement or input statement inside a do loop?

Your understandings are basically correct ... just a few notes.

As data_null_ indicated, you can add to any of these steps:

newvar=_n_;

If you add it at the end of the DATA step, it might help you understand more.  _n_ counts the number of iterations of the DATA step.

In your Example #3, I'm assuming you meant to add END=LAST to the SET statement.

Example #4 is actually much more complicated, because the unspecified SAS statements might affect the number of iterations.  For example, suppose they included:

if name='Mark';

Each mismatch on the subsetting IF ends that iteration of the DATA step.

Good luck.

Regular Contributor
Posts: 180

Re: How many times does the data step iterate when using set statement or input statement inside a do loop?

The number of times the data step iterates in example 1 and 2 depends on the number of observations the have data set has:

N_iterations = ceil(observation_of_have_dataset  / 10)

The number of iterations in Example 4 is the number of ID groups.

CTorres

Discussion stats
• 8 replies
• 464 views
• 4 likes
• 6 in conversation