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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 918 views
  • 0 likes
  • 3 in conversation