BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
eemrun
Obsidian | Level 7

SAS rookie here. i am trying to calculate default and redefault rate for a  portfolio. I have the following logic to work through

 

Cure is defined as an account being in good health for 6 months following a default.So if an account is in default(denoted as 1) at time 1, then if there are no more defaults till time 6, then at time 7, account is said to be 'Cured' (denoted as Cured = 1).  Default is satisfied if one of the two flag variables are true (FirstDefaultFlag, DefaultFlag). An account can get multiple defaults and cures. I just need to flag them. I have the following code but obviously it is incorrect. 

 

data p1;
set p0;
by AccountID;

If DefaultFlag = 'Y' or FirstDefaultFlag = 'Y' then Default = 1;
else Default = 0;

if Default = 1 then do;
row = 0;
selected = 0;
cure = 0;
end;
row + 1;
retain selected;
if selected = 0 and row <= 6 and Default = 0 then do;
Cure = 1;
selected = 1;
end;
drop row selected;
run;



 

Any helpers?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

I can't test this right now, so I have to leave that part to you.  This should at least be a good attempt:

 

data p1;

set p0;

by AccountID;

if first.AccountID then do;

   n_months=0;

   cure=0;

end;

retain n_months cure;

if DefaultFlag='Y' or FirstDefaultFlag='Y' then n_months=1;

else if n_months > 0 then n_months + 1;

if n_months=7 then do;

   cure = 1;

   n_months = 0;

end;

run;

 

As a rookie, you might learn to use a BY statement in a DATA step.  It creates first.ByVariable and last.ByVariable automatically.  Those are flags that tell you when a group of observations begins or ends.  Very important tool.

View solution in original post

4 REPLIES 4
Astounding
PROC Star

I can't test this right now, so I have to leave that part to you.  This should at least be a good attempt:

 

data p1;

set p0;

by AccountID;

if first.AccountID then do;

   n_months=0;

   cure=0;

end;

retain n_months cure;

if DefaultFlag='Y' or FirstDefaultFlag='Y' then n_months=1;

else if n_months > 0 then n_months + 1;

if n_months=7 then do;

   cure = 1;

   n_months = 0;

end;

run;

 

As a rookie, you might learn to use a BY statement in a DATA step.  It creates first.ByVariable and last.ByVariable automatically.  Those are flags that tell you when a group of observations begins or ends.  Very important tool.

eemrun
Obsidian | Level 7

This is great! Thanks so much for the quick response. 

 

I had one small query. When I run the code, I notice that once an account has a cure, the subsequent cure values remain as 1 even when it has gone back to redefault. Any idea how I can reset the count so that once an account has a cure of 1, the next month, it will go back to zero as is the case with n_months. 

Astounding
PROC Star

The easiest way would be to add a condition to the existing IF/THEN statement:

 

if cure=1 or first.AccountID then do;

 

Once that is in place, the last four statements can be replaced with just one statement:

 

if n_months=7 then cure = 1;

eemrun
Obsidian | Level 7
Got it. Thanks so much!

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1679 views
  • 2 likes
  • 2 in conversation