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

Hi,

 

I have the following dataset

 

 

ID

Month

Arrears

1

Jan

0

1

Feb

0

1

Mar

1

1

Apr

2

1

May

2

2

Jan

0

2

Feb

1

2

Mar

1

2

Apr

0

2

May

0

 

What I need is to create a flag that counts the number of months since an account was 1 plus in arrears until it returns to 0.

 

IDMonthArrearsFlag
1Jan00
1Feb00
1Mar11
1Apr22
1May23
2Jan00
2Feb11
2Mar12
2Apr00
2May00

 

As always any help would be greatly appreciated.

 

Adnan

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data have;
input ID$ Month$ Arrears;
datalines;
1 Jan 0
1 Feb 0 
1 Mar 1
1 Apr 2
1 May 2
2 Jan 0
2 Feb 1
2 Mar 1
2 Apr 0
2 May 0
;

proc sort data = have;
   by ID;
run;

data want;
   set have;
   by ID;
   if first.ID then flag = 0;
   if Arrears > 0 then flag + 1;
   else if Arrears = 0 then flag = 0;
run;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20
data have;
input ID$ Month$ Arrears;
datalines;
1 Jan 0
1 Feb 0 
1 Mar 1
1 Apr 2
1 May 2
2 Jan 0
2 Feb 1
2 Mar 1
2 Apr 0
2 May 0
;

proc sort data = have;
   by ID;
run;

data want;
   set have;
   by ID;
   if first.ID then flag = 0;
   if Arrears > 0 then flag + 1;
   else if Arrears = 0 then flag = 0;
run;
Astounding
PROC Star

While this solution is close, I think you need to tweak it.  This statement should be added after the BY statement:

 

if first.id then flag=0;

 

 

Adnan_Razaq
Calcite | Level 5

Thanks for providing the solution.

 

I have another query stemming of the one raised.

 

I need to set up a count of months since the account was last 1+ in arrears. How can I set up a flag to do the following

 

Acc    Date     Arrears      Since last 1+

1         Jan        0                    0

1         Feb        1                    0

1         Mar        2                    0

1         Apr         0                    1

1         Jun         0                    2

1         Jul          1                    0

1         Aug         0                   1  

 

 

Many Thanks

 

Adnan

ballardw
Super User

Without a year value I expect this process to have issues unless your data order is maintained very rigorously AND is never sorted.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 5 replies
  • 1783 views
  • 0 likes
  • 4 in conversation