DATA Step, Macro, Functions and more

Question about "retain" statement

Accepted Solution Solved
Reply
Contributor
Posts: 34
Accepted Solution

Question about "retain" statement

Hi,

 

I inherited someone's code and am perplexed as I'm not sure why it is behaving this way. I'd appreciate if someone can provide some insight. The code is below. Below code is the output of the "putlog" statement.

 

As you can see, the code is trying out count occurrence of loan IDs in the "modpop_s_EverNev" input dataset, which is captured in the field "prev_mod_count" of the "modpop_s_EverNev_1" output dataset. As the "putlog" statement output shows that this loan occured 4 times, so that is why the value of "prev_mod_count" is 4.

 

My question is that how is the value of "prev_mod_count" retained across observation when it is not specified in the "retain" statment? As only "first_mod" is retained, shouldn't "prev_mod_count" reset with 2nd, 3rd and 4th observations of the "modpop_s_EverNev" input dataset?

 

=======Code=========

 

data modpop_s_EverNev_1;
    length prev_mod_count first_mod 3;
    set modpop_s_EverNev(where=(ln_id='fake loan ID'));
    by ln_id mod_start_dte;

    retain first_mod;

    ***Reset counters for every loan***;
    if first.ln_id then do;
       first_mod = 0;
        prev_mod_count = 0;
   end;

    /*
        We want to identify only those mods as the first mod where
        the payment reduction was 10% or more
    */
    if .<pct_pay_chg<=-10 then
        first_mod=1;
    /*
        After a loan has been determined to be modified, if it modifies again,
        increase the counter.
    */
   if first_mod = 1 then do;
        prev_mod_count + 1;
        putlog mod_start_dte= first_mod= prev_mod_count=;
    end;
    if last.ln_ID then
        output modpop_s_EverNev_1;     
run;

 

 

=======Putlog output=========

MOD_START_DTE=*** first_mod=1 prev_mod_count=1
MOD_START_DTE=*** first_mod=1 prev_mod_count=2
MOD_START_DTE=*** first_mod=1 prev_mod_count=3
MOD_START_DTE=*** first_mod=1 prev_mod_count=4

NOTE: There were 4 observations read from the data set WORK.MODPOP_S_EVERNEV.
      WHERE ln_id='fake loan id';
NOTE: The data set WORK.MODPOP_S_EVERNEV_1 has 1 observations and 9 variables.
NOTE: DATA statement used (Total process time):
      real time           0.06 seconds
      cpu time            0.05 seconds


Accepted Solutions
Solution
‎05-12-2016 04:38 PM
Trusted Advisor
Posts: 1,117

Re: Question about "retain" statement

[ Edited ]

Hi @asimraja,

 

Please note that the sum statement "variable + increment;", as in prev_mod_count + 1, implies a RETAIN for the variable.

 

Edit: Here is the link to the documentation of the sum statement, where you can find the hint in the "Tips" on argument "variable."

View solution in original post


All Replies
Solution
‎05-12-2016 04:38 PM
Trusted Advisor
Posts: 1,117

Re: Question about "retain" statement

[ Edited ]

Hi @asimraja,

 

Please note that the sum statement "variable + increment;", as in prev_mod_count + 1, implies a RETAIN for the variable.

 

Edit: Here is the link to the documentation of the sum statement, where you can find the hint in the "Tips" on argument "variable."

Contributor
Posts: 34

Re: Question about "retain" statement

Posted in reply to FreelanceReinhard

Ah, I got it. Thank you for pointing it out!

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 204 views
  • 0 likes
  • 2 in conversation