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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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