BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lightmiker
Calcite | Level 5

Recently i've discovered some strange behavior (at least for me) of SET statement in some special situation. Consider two datasteps:

1st)

data New;

     do i = 1 to 2;

          set Old;

     end;

run;

2nd)

data New;

     set Old;

     set Old;

run;

The stages of 1st datastep execution are clear for me. In do-loop SET statement reads odd observation firstly and even observation secondly. Both observations are read to the same PDV, that's why NEW contains only even observations.

I thought that second datastep must work just the same because it's just unrolled version of the first datastep. But it works differently. NEW and OLD datasets are equal. It doesn't matter how much "set Old;" statements to write in second datastep, result is just the same. For some reason SET statement doesn't move some kind of pointer over the dataset for that style of coding. I haven't found any explanation of such behavior on help pages or anywhere else. Hope that someone can explain it.

Cheers,

Mike

1 ACCEPTED SOLUTION

Accepted Solutions
gergely_batho
SAS Employee

When you have 2 (or more) set statements in a data step, each of them mainains its own pointer.

This might be very usefull, for example if you want to read 2 different portions of your data at the same time.

You can also see in the log, that data step reads the input data 2 times.

View solution in original post

3 REPLIES 3
gergely_batho
SAS Employee

When you have 2 (or more) set statements in a data step, each of them mainains its own pointer.

This might be very usefull, for example if you want to read 2 different portions of your data at the same time.

You can also see in the log, that data step reads the input data 2 times.

lightmiker
Calcite | Level 5

I thought that pointer is connected to dataset and SET statements just move it one by one. Now I see that each separate SET statement "reopens" the dataset for reading with its own pointer. And statement in loop is considered as single statement during iterations. Hope i've got it in right way. Thank you for quick reply!

Kurt_Bremser
Super User

As Gergely said, each set statement keeps it's own pointer.

The data step iterates like this:

(1st set) read contents of first record of old into PDV.

(2nd set) read contents of first record of old into PDV. Since Variables have the same name, values are overwritten.

write contents of PDV to new.

go to beginning.

repeat until EOF in both set statements.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 1151 views
  • 0 likes
  • 3 in conversation