Help using Base SAS procedures

Strange behavior of SET statement

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Strange behavior of SET statement

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


Accepted Solutions
Solution
‎01-16-2015 03:30 AM
SAS Employee
Posts: 340

Re: Strange behavior of SET statement

Posted in reply to lightmiker

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


All Replies
Solution
‎01-16-2015 03:30 AM
SAS Employee
Posts: 340

Re: Strange behavior of SET statement

Posted in reply to lightmiker

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.

Occasional Contributor
Posts: 5

Re: Strange behavior of SET statement

Posted in reply to gergely_batho

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!

Super User
Posts: 7,782

Re: Strange behavior of SET statement

Posted in reply to lightmiker

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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