turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Question about "retain" statement

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-12-2016 03:48 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-12-2016 03:59 PM - edited 05-12-2016 04:03 PM

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*."

All Replies

Solution

05-12-2016
04:38 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-12-2016 03:59 PM - edited 05-12-2016 04:03 PM

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*."

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-12-2016 04:38 PM

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