<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Nr months with neg balance in row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Nr-months-with-neg-balance-in-row/m-p/954437#M372746</link>
    <description>&lt;P&gt;Below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	by CustID YYYYMM;
	retain max_min cons_min;
	if balance&amp;gt;=0 then cons_min=0;
	else cons_min + 1;
	max_min=max(max_min,cons_min);
	if last.CustID then
		do;
			output;
			call missing(cons_min, max_min);
		end;
	keep CustID max_min;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 22 Dec 2024 08:55:54 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2024-12-22T08:55:54Z</dc:date>
    <item>
      <title>Nr months with neg balance in row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nr-months-with-neg-balance-in-row/m-p/954419#M372741</link>
      <description>&lt;P&gt;Hello&lt;BR /&gt;For each customer I have 12 rows (12 months data) with information of current account balance.&lt;BR /&gt;I want to calculate for each customer:&amp;nbsp; Maximum number of months with negative balance in a row (It means that month after month had negative balance).&lt;BR /&gt;In this example:&lt;BR /&gt;Customer 111 has 3 months in row with negative balance&lt;BR /&gt;Customer 222 has 0 months in row with negative balance&lt;BR /&gt;What is the way to calculate it in sas please ?&lt;BR /&gt;Please see a code that calculate it well but maybe there is other nice code&amp;nbsp; ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CustID YYYYMM balance;
IF balance&amp;lt;0 then Ind_Neg=1;else Ind_Neg=0;
cards;
111 2020401 1000
111 2020402 -4000
111 2020403 1000
111 2020404 1900
111 2020405 -1000
111 2020406 -2000
111 2020407 5000
111 2020408 6000
111 2020409 -2000
111 2020410 -3000
111 2020411 -4000
111 2020412 8000
222 2020401 3000
222 2020402 4000
222 2020403 1000
222 2020404 5000
222 2020405 8000
222 2020406 7800
222 2020407 5000
222 2020408 3000
222 2020409 1000
222 2020410 2000
222 2020411 1000
222 2020412 7000
;
Run;

proc sort data=have;
by CustID YYYYMM; 
run;
data want1;
set have;
by CustID;
retain Accum_Nr_Neg;
if first.CustID then Accum_Nr_Neg=Ind_Neg;
else Accum_Nr_Neg+Ind_Neg;
run;
data want2;
set have;
by CustID;
retain Accum_Nr_Neg_in_Row;
if first.CustID then Accum_Nr_Neg_in_Row=Ind_Neg;
else Accum_Nr_Neg_in_Row=sum(Accum_Nr_Neg_in_Row,Ind_Neg)*Ind_Neg;
run;
proc sql;
create table want3 as
select CustID,
       max(Accum_Nr_Neg_in_Row) as max_Accum_Nr_Neg_in_Row
from want2
group by CustID
;
quit;

  

 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 21 Dec 2024 20:33:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nr-months-with-neg-balance-in-row/m-p/954419#M372741</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-12-21T20:33:43Z</dc:date>
    </item>
    <item>
      <title>Re: Nr months with neg balance in row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nr-months-with-neg-balance-in-row/m-p/954420#M372742</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    by custid;
    if first.custid or balance&amp;gt;=0 then consec=0;
    if balance&amp;lt;0 then consec+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then use PROC SUMMARY to find the max of CONSEC by CUSTID.&lt;/P&gt;</description>
      <pubDate>Sat, 21 Dec 2024 21:03:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nr-months-with-neg-balance-in-row/m-p/954420#M372742</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-12-21T21:03:13Z</dc:date>
    </item>
    <item>
      <title>Re: Nr months with neg balance in row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nr-months-with-neg-balance-in-row/m-p/954437#M372746</link>
      <description>&lt;P&gt;Below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	by CustID YYYYMM;
	retain max_min cons_min;
	if balance&amp;gt;=0 then cons_min=0;
	else cons_min + 1;
	max_min=max(max_min,cons_min);
	if last.CustID then
		do;
			output;
			call missing(cons_min, max_min);
		end;
	keep CustID max_min;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 22 Dec 2024 08:55:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nr-months-with-neg-balance-in-row/m-p/954437#M372746</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-12-22T08:55:54Z</dc:date>
    </item>
    <item>
      <title>Re: Nr months with neg balance in row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nr-months-with-neg-balance-in-row/m-p/954627#M372810</link>
      <description>&lt;P&gt;Once the data are sorted by custid yyyymm (and no gaps in YYYYMM), then a RETAIN statement provides a nice way to dynamically update the maximum consecutive qualifying balances, and output the result for each custid.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=custid max_consec);
  set have;
  by custid yyyymm;
  retain max_consec;

  if first.custid=1 or balance&amp;gt;0 then call missing(consec,max_consec);
  if sign(balance)=-1 then consec+1;
  else consec=0;
  max_consec=max(max_consec,consec);
  if last.custid;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Dec 2024 03:32:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nr-months-with-neg-balance-in-row/m-p/954627#M372810</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-12-26T03:32:30Z</dc:date>
    </item>
    <item>
      <title>Re: Nr months with neg balance in row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nr-months-with-neg-balance-in-row/m-p/954880#M372946</link>
      <description>&lt;P&gt;Numeric variable not a date value. 7 digits. So which of 2020401 is the "month".&lt;/P&gt;
&lt;P&gt;With out a very clear definition of what that variable YYYYMM represents and how I wouldn't touch of this with code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello&lt;BR /&gt;For each customer I have 12 rows (12 months data) with information of current account balance.&lt;BR /&gt;I want to calculate for each customer:&amp;nbsp; Maximum number of months with negative balance in a row (It means that month after month had negative balance).&lt;BR /&gt;In this example:&lt;BR /&gt;Customer 111 has 3 months in row with negative balance&lt;BR /&gt;Customer 222 has 0 months in row with negative balance&lt;BR /&gt;What is the way to calculate it in sas please ?&lt;BR /&gt;Please see a code that calculate it well but maybe there is other nice code&amp;nbsp; ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CustID YYYYMM balance;
IF balance&amp;lt;0 then Ind_Neg=1;else Ind_Neg=0;
cards;
111 2020401 1000
111 2020402 -4000
111 2020403 1000
111 2020404 1900
111 2020405 -1000
111 2020406 -2000
111 2020407 5000
111 2020408 6000
111 2020409 -2000
111 2020410 -3000
111 2020411 -4000
111 2020412 8000
222 2020401 3000
222 2020402 4000
222 2020403 1000
222 2020404 5000
222 2020405 8000
222 2020406 7800
222 2020407 5000
222 2020408 3000
222 2020409 1000
222 2020410 2000
222 2020411 1000
222 2020412 7000
;
Run;

proc sort data=have;
by CustID YYYYMM; 
run;
data want1;
set have;
by CustID;
retain Accum_Nr_Neg;
if first.CustID then Accum_Nr_Neg=Ind_Neg;
else Accum_Nr_Neg+Ind_Neg;
run;
data want2;
set have;
by CustID;
retain Accum_Nr_Neg_in_Row;
if first.CustID then Accum_Nr_Neg_in_Row=Ind_Neg;
else Accum_Nr_Neg_in_Row=sum(Accum_Nr_Neg_in_Row,Ind_Neg)*Ind_Neg;
run;
proc sql;
create table want3 as
select CustID,
       max(Accum_Nr_Neg_in_Row) as max_Accum_Nr_Neg_in_Row
from want2
group by CustID
;
quit;

  

 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jan 2025 02:07:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nr-months-with-neg-balance-in-row/m-p/954880#M372946</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2025-01-02T02:07:02Z</dc:date>
    </item>
  </channel>
</rss>

