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

I need to count how many accounts having the same negative balance 3 or more months in a row in year 2014.

Example:

Customer   Month        Balance    Count

A               201401      100          0

A               201402       -30          1

A               201403       -30          2

A               201404       -30          3

A               201405       -10          0

Can someone advise how to code it? Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

Hi Art,

Thanks again for your feedback. Right, it will not pick adjacent balances. I've created a new variable flag that will group adjacent balances.

Regards,

proc sort data=have;

by customer;

run;

data want;
set have;
by customer balance notsorted;
if first.customer then flag=0;
if first.balance then flag+1;
run;

proc sql;
select distinct customer from want
where balance < 0
group by customer,flag
having range(balance)=0
and count(balance)>=3;
quit;

View solution in original post

9 REPLIES 9
art297
Opal | Level 21

data want (keep=customer);

  retain counter .

  retain keep;

  array stack {0:2};

  retain stack:;

  set have;

  by customer;

  if first.customer then do;

    call missing(of stack{*});

    counter=0;

    keep=0;

  end;

  counter+1;

  stack{mod(counter,3)} = Balance;

  if counter gt 2 then if stack{0}< 0 and

   (stack{0}=stack{1}=stack{2}) then keep=1;

  if last.customer and keep then output;

run;

proc sql;

  select count(*) as total

    from want

  ;

quit;

stat_sas
Ammonite | Level 13

Another way using sql.

proc sql;

select customer from have

where balance<0

group by customer,balance

having range(balance)=0

and count(balance)>=3;

quit;

art297
Opal | Level 21

stat@sas: the sql approach you suggested wouldn't assure that the 3 negative balances were adjacent.

stat_sas
Ammonite | Level 13

Hi Art,

Thanks for the input.  "Group by customer,balance" in proc sql code will make sure adjacent numbers. Please advice.

Regards,

art297
Opal | Level 21

No, I don't think so.  Group by customer balance will simply put all of the similar balances together regardless of whether they were, or weren't, adjacent in the first place.

stat_sas
Ammonite | Level 13

Hi Art,

Thanks again for your feedback. Right, it will not pick adjacent balances. I've created a new variable flag that will group adjacent balances.

Regards,

proc sort data=have;

by customer;

run;

data want;
set have;
by customer balance notsorted;
if first.customer then flag=0;
if first.balance then flag+1;
run;

proc sql;
select distinct customer from want
where balance < 0
group by customer,flag
having range(balance)=0
and count(balance)>=3;
quit;

otis
Calcite | Level 5

Thank you, stat@sas and Art. Both of your codes worked beautifully. I have saved both codes in my library.

art297
Opal | Level 21

FWIW: I'd also save data_null_'s suggested code as, IMHO, it was the most efficient of the three solutions.

data_null__
Jade | Level 19

Your test data is a bit "thin".  This might work but you don't explain what you want well enough for me.

data same;
   input Customer :$1. Month $    Balance;
   cards;
A      201401   100
A      201402   -30
A      201403   -30
A      201404   -30
A      201405   -10
;;;;
   run;
data same2;
   set same;
   by customer balance notsorted;
  
if first.balance then count=0;
  
if sign(balance) eq -1 then do;
     
if not (first.balance and last.balance) then count+1;
     
if last.balance and count ge 3 then output;
     
end;
  
run;
proc print;
  
run;

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 9 replies
  • 1119 views
  • 6 likes
  • 4 in conversation