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
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.
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.
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!
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.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: