Solved
Contributor
Posts: 37

Conditional Arguments

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?

Accepted Solutions
Solution
‎07-03-2017 08:44 PM
Super User
Posts: 6,905

Re: Conditional Arguments

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.

All Replies
Solution
‎07-03-2017 08:44 PM
Super User
Posts: 6,905

Re: Conditional Arguments

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.

Contributor
Posts: 37

Re: Conditional Arguments

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.

Super User
Posts: 6,905

Re: Conditional Arguments

[ Edited ]

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;

Contributor
Posts: 37