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.

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!

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.

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