BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
vijaypratap0195
Obsidian | Level 7

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:

vijaypratap0195_0-1693501909081.png

 

Output code 2: 

vijaypratap0195_1-1693501916715.png

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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.

Ksharp_0-1693568286840.png

 

View solution in original post

5 REPLIES 5
Quentin
Super User

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;
The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Tom
Super User Tom
Super User

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.

ballardw
Super User

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

mkeintz
PROC Star

@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

--------------------------
Ksharp
Super User
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.

Ksharp_0-1693568286840.png

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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