DATA Step, Macro, Functions and more

automatically retained

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

automatically retained

Hi SAS experts,

 

when I ran the following program, I found y was automatically retained for x=4 in data test2. What happed?

 

Thanks!

 

data test;
    input x y;
    cards;
    1 .
    3 .
    ;
run;

data test1;
    input x;
    cards;
    3
    4
    ;
run;
data test2;
    set test(in=a)
        test1(in=b);
    if b then do;
        if x=3 then y=4;
    end;
run;


Accepted Solutions
Solution
‎08-23-2017 09:38 AM
Super User
Posts: 5,513

Re: automatically retained

Posted in reply to fbl204653

Any variable that comes from any SAS data set is automatically retained.  Since Y comes from a SAS data set, it is automatically retained.

 

A small complication:  when switching data sets (from TEST to TEST1 in the SET statement), Y is reset to missing.  You can see that by adding this statement after the SET statement:

 

put Y;

 

But once Y is set to 4, it remains 4 (unless changed by subsequent programming statements).  As @PGStats said, reading an observation from TEST1 does not replace Y. 

View solution in original post


All Replies
Respected Advisor
Posts: 4,927

Re: automatically retained

Posted in reply to fbl204653

There is no y variable in dataset test1 so the value of y isn't reset to missing when test1 is read.

PG
Contributor
Posts: 36

Re: automatically retained

Thanks!

Respected Advisor
Posts: 4,927

Re: automatically retained

Posted in reply to fbl204653

My answer is not quite right. You should mark @Astounding answer as the correct answer instead.

PG
Solution
‎08-23-2017 09:38 AM
Super User
Posts: 5,513

Re: automatically retained

Posted in reply to fbl204653

Any variable that comes from any SAS data set is automatically retained.  Since Y comes from a SAS data set, it is automatically retained.

 

A small complication:  when switching data sets (from TEST to TEST1 in the SET statement), Y is reset to missing.  You can see that by adding this statement after the SET statement:

 

put Y;

 

But once Y is set to 4, it remains 4 (unless changed by subsequent programming statements).  As @PGStats said, reading an observation from TEST1 does not replace Y. 

Contributor
Posts: 36

Re: automatically retained

Posted in reply to Astounding
Thanks!
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 94 views
  • 2 likes
  • 3 in conversation