Obsidian | Level 7

## Set; inside loop in SAS in two different ways

Can anyone explain to me the logic behind these 2 codes please (uses set in loop):

Code 1:

``````Data class1 ;
Do I=1 to 2 ;
Set sashelp.class;
End;
Run;
proc print; run;``````

Code 2:

`Data class1 ; Do I=1 to 3 ; Set sashelp.class; End;Run;proc print; run;`

Output code 1:

Output code 2:

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Set; inside loop in SAS in two different ways

```You could take code

Data class1 ;
Do I=1 to 2 ;
Set sashelp.class;
End;
Run;

As this

Data class1 ;
Do I=1 to 2 ;
Set sashelp.class;
End;
output;   /*<--*/
Run;

Therefore, it would output the 2nd,4th,6th,8th,10th,12th..... obs.```

5 REPLIES 5
PROC Star

## Re: Set; inside loop in SAS in two different ways

One way to explore this is to add PUT statements, showing the value of _N_ and other variables.

For example, try running:

``````data class1 ;
do i=1 to 2 ;
set sashelp.class;
put "Inside Loop" (_N_ i Name)(=) ;
end;
put "Outside Loop" (_N_ i Name)(=) ;
run;``````
Check out the Boston Area SAS Users Group (BASUG) video archives: https://www.basug.org/videos.
Super User

## Re: Set; inside loop in SAS in two different ways

Since you read 2 (or 3) observations but output one it seems pretty obvious what it is doing.

Notice how the 3rd observation in the first run matches the 2nd observation in the second run.  Confirming the mathematical equality that 2*3 is the same as 3*2.

Super User

## Re: Set; inside loop in SAS in two different ways

If you are trying to examine the behavior of multiple Set statements I recommend using multiple small data sets of different sizes, small number of variables, some with different names and (at least some) values unique to the data set.

That way have some chance of possibly seeing which actual set contributed which record. Plus you may get an education of what happens with sets and different numbers of observations.

If the question is about the value of I

## Re: Set; inside loop in SAS in two different ways

@vijaypratap0195 wrote:

Can anyone explain to me the logic behind these 2 codes please (uses set in loop):

I'm not entirely sure of what you are asking, but ...

1. Remember, that in the absence of an explicit OUTPUT statement, there is an implicit one just prior to the RUN; statement.

2. SETs inside a do loop read a succession of observations until the loop conditions are satisfied.  But only the last observation read by the loop is in memory at the end of the loop, and therefore exposed to the implicit OUTPUT.

3. As a result, your first example outputs every even-numbered original observation, and the second outputs every 0mod3 obs.  Note that since the original dataset has 19 obs, in both cases the end of sashelp.class will be encountered before the end of the loop.  So the data step will immediately stop before reaching the  implicit OUTPUT and obs 19 is not in the resulting data set.

In general, using SETs inside a DO loop is done for purposes of aggregating a series of obs, often a having a single ID.  Once the aggregation calculations are done, often a second use of SET within a DO loop is done to reread (and explicitly OUTPUT) the same obs, but with additional group-related information.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Super User

## Re: Set; inside loop in SAS in two different ways

```You could take code

Data class1 ;
Do I=1 to 2 ;
Set sashelp.class;
End;
Run;

As this

Data class1 ;
Do I=1 to 2 ;
Set sashelp.class;
End;
output;   /*<--*/
Run;

Therefore, it would output the 2nd,4th,6th,8th,10th,12th..... obs.```

Discussion stats
• 5 replies
• 212 views
• 0 likes
• 6 in conversation