Hi
when I am using the below code. I get wrong result.
data a1;
x='abc';
run;
data a2;
y='y';output;
y='';output;
run;
data a3;
set a1 a2;
if y='y' then x='yes';
run;
Since in the third obs of dataset a3, y= '', I expect to get the result x='', but in fact, the result is x='yes'.
What happens when the SET statement misbehaves—and how you can fix it!
By Kim Wilson on SAS Users February 21, 2014
https://blogs.sas.com/content/sgf/2014/02/21/what-happens-when-the-set-statement-misbehaves-and-how-...
KB0041936
Variables read using SET, MERGE, and UPDATE statements are automatically retained
https://sas.service-now.com/csm/en/variables-read-using-set-merge-and-update-statements-are-automati...
You get the results you want - with same number of steps - when you do something like this:
data a1;
x='abc';
run;
data a2;
y='y'; x=''; output;
y='' ; x=''; output;
run;
data a3;
set a1 a2;
if y='y' then x='yes';
run;
/* end of program */
BR, Koen
In short ... to understand why you get the results you get ... you have to consider what is happening in the program data vector (PDV).
When using a SET statement, the values are automatically retained until the next observations value is written to the program data vector. The program data vector is created at compile time.
In your code, the variables X and Y do not exist in both data sets but when the program data vector is built, it sees both variables and marks them to RETAIN because they exist in one of the data set where you SET them together.
When you create the X and Y variables on both data sets ... you will get the desired results.
BR, Koen
Hi,
I see the expected result. In the second observation the value is "yes" but it's empty in obs 1 and 3.
If the DATA step isn't following the logic you expect, I recommend using the DATA step debugger (available in EG and in SAS Studio within Viya) to step through.
Firstly , I should claim
the variable X1 is from dataset A1, so
x='yes'
is retained for the next all obs.
The key point is all the variables from datasets would be retained .
So if you want to get right result , you should reset variable X .
data a1; x='abc'; run; data a2; y='y';output; y='';output; run; data a3; set a1 a2(in=ina2); if ina2 then call missing(x); if y='y' then x='yes'; run;
Nearly 200 sessions are now available on demand in the Innovate Hub.
Watch Now →Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.