@odahviing I simply copy/pasted your question as-is into Copilot. I've only "browsed" through the answer but it looks right to me and provides certainly much more detail than I could - and certainly explains things in much more detail than I could.
You're on the right track! The behaviour you're observing is indeed related to how the Program Data Vector (PDV) is managed in SAS when using the SET statement.
Here's a step-by-step explanation:
Creating c1 , c2 , and c3 :
c1 is created by copying all observations from sashelp.class .
c2 is created by copying the first 3 observations from c1 and keeping only the name variable.
c3 is created by copying the first 3 observations from c1 and keeping only the age variable.
Creating c_out :
The PUT statement outputs the current values of all variables in the PDV.
The first SET statement reads from c2 and c3 . When it reads from c2 , it initializes the name variable and sets age to missing. When it reads from c3 , it initializes the age variable and sets name to missing.
The second SET statement reads from c3 and c2 . When it reads from c3 , it initializes the age variable and sets name to missing. When it reads from c2 , it initializes the name variable and sets age to missing.
The key point is that each SET statement reinitializes the PDV, setting variables not in the current dataset to missing. This is why you see missing values for name in the first observation and age in the fourth observation.
Here's a simplified breakdown of the c_out dataset creation:
First Observation:
SET c2 c3 : Reads from c2 , initializes name , sets age to missing.
SET c3 c2 : Reads from c3 , initializes age , sets name to missing.
Second Observation:
SET c2 c3 : Reads from c2 , initializes name , sets age to missing.
SET c3 c2 : Reads from c2 , initializes name , sets age to missing.
Third Observation:
SET c2 c3 : Reads from c3 , initializes age , sets name to missing.
SET c3 c2 : Reads from c3 , initializes age , sets name to missing.
Fourth Observation:
SET c2 c3 : Reads from c3 , initializes age , sets name to missing.
SET c3 c2 : Reads from c2 , initializes name , sets age to missing.
I hope this clarifies the behavior you're seeing! If you have any more questions or need further explanation, feel free to ask.
... View more