<?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: Count numbers of the same value with sequence in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-numbers-of-the-same-value-with-sequence/m-p/835896#M330497</link>
    <description>&lt;P&gt;So I do nested loop to count the series of number (TT) with the same value.&lt;/P&gt;
&lt;P&gt;The basic idea is:&lt;/P&gt;
&lt;P&gt;Start with 1st row. Keep TT value in &lt;CODE class=" language-sas"&gt;value_series_1 &lt;/CODE&gt;.&lt;/P&gt;
&lt;P&gt;Loop to next row, if TT is the same as&amp;nbsp;&lt;CODE class=" language-sas"&gt;value_series_1 &lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;increase the&amp;nbsp;&lt;CODE class=" language-sas"&gt;N_series_1  &lt;/CODE&gt;by 1. If not the same, it is the start of &lt;CODE class=" language-sas"&gt;value_series_2 &lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;My code below work with lookback=12. It gives correct result for the first few rows but then at rows 8, number look weird with the N_series inflated to 30+.&lt;/P&gt;
&lt;P&gt;Same thing when lookback=4, the error show right on the first row.&lt;/P&gt;
&lt;P&gt;Look like the script doesn't stop at i+&amp;amp;lookback&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DO j=i+1 to i+&amp;amp;lookback until (End_run=1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I can't find the problem and really appreciate it if you could help.&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID TT;
datalines;
1 500
1 500
1 3
1 3
1 3
1 -3
1 -3
1 -500
1 500
1 -5
1 -500
1 3
1 3
;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;
%let lookback=5;


data want;
set have;
drop i End_run ID1-ID5 TT1-TT5;
i+1;
End_run=0;
N_series_1 = 0; value_series_1 =0;
N_series_2 = 0; value_series_2 =0;
N_series_3 = 0; value_series_3 =0;
N_series_4 = 0; value_series_4 =0;
N_series_5 = 0; value_series_5 =0;


N_series_1 = 1; value_series_1 =TT;
DO j=i+1 to i+&amp;amp;lookback until (End_run=1);

	set have(keep = ID TT rename =(ID=ID1 TT=TT1)) point=j nobs=nobs1;
	if TT1=value_series_1 then N_series_1=N_series_1+1; 
	else 
	if TT1^=value_series_1 then do; 
		N_series_2=N_series_2+1; value_series_2=TT1;
		DO k=j+1 to i+&amp;amp;lookback until (End_run=1);

		set have(keep = ID TT rename =(ID=ID2 TT=TT2)) point=k nobs=nobs2;
			if TT2=value_series_2 then N_series_2=N_series_2+1; 
			else 
			if TT2^=value_series_2 then do; 
				N_series_3=N_series_3+1; value_series_3=TT2;
				DO l=k+1 to i+&amp;amp;lookback until (End_run=1);

				set have(keep = ID TT rename =(ID=ID3 TT=TT3)) point=l nobs=nobs3;
					if TT3=value_series_3 then N_series_3=N_series_3+1; 
					else 
					if TT3^=value_series_3 then do; 
						N_series_4=N_series_4+1; value_series_4=TT3;
						DO m=l+1 to i+&amp;amp;lookback until (End_run=1);

						set have(keep = ID TT rename =(ID=ID4 TT=TT4)) point=m nobs=nobs4;
							if TT4=value_series_4 then N_series_4=N_series_4+1; 
							else 
							if TT4^=value_series_4 then do; 


						N_series_5=N_series_5+1; value_series_5=TT4;
						DO n=m+1 to i+&amp;amp;lookback until (End_run=1);

						set have(keep = ID TT rename =(ID=ID5 TT=TT5)) point=n nobs=nobs5;
							if TT5=value_series_5 then N_series_5=N_series_5+1; 
							else if TT5^=value_series_5 then do; 
							End_run=1;end;
END;END;END;END;;END;END;END;END;END;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 29 Sep 2022 17:31:15 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2022-09-29T17:31:15Z</dc:date>
    <item>
      <title>Count numbers of the same value with sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-numbers-of-the-same-value-with-sequence/m-p/835767#M330435</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;I have data of 2 columns ID and T.&lt;/P&gt;
&lt;P&gt;T have value of -500, -3, 0, 3, 500.&lt;/P&gt;
&lt;P&gt;For a give row, I want to look back, say 12 rows and “count how many of T having the same &lt;STRONG&gt;Non-Zero&lt;/STRONG&gt; value Before T change to a different non-zero value”.&lt;/P&gt;
&lt;P&gt;In the data below, for the 1 row I see:&lt;/P&gt;
&lt;P&gt;1&lt;SUP&gt;st&lt;/SUP&gt; &lt;STRONG&gt;series_value&lt;/STRONG&gt; is 500 and has 2 counts and the &lt;STRONG&gt;SUM_series1&lt;/STRONG&gt; is 1000&lt;/P&gt;
&lt;P&gt;2&lt;SUP&gt;nd&lt;/SUP&gt; series_value is 3 and has 3 counts and the SUM_series2 is 9&lt;/P&gt;
&lt;P&gt;3rd series_value is -3 and has 2 counts and the SUM_series3 is -6&lt;/P&gt;
&lt;P&gt;4&lt;SUP&gt;th&lt;/SUP&gt; series_value is -500 and has 1 count and the SUM_series4 is -500&lt;/P&gt;
&lt;P&gt;5&lt;SUP&gt;th&lt;/SUP&gt; series_value is &amp;nbsp;500 and has 1 count and the SUM_series5 is 500&lt;/P&gt;
&lt;P&gt;In the count, clearly 0 can be removed but I still need it to keep the track of the looking back.&lt;/P&gt;
&lt;P&gt;I only need up to 5&lt;SUP&gt;th&lt;/SUP&gt; series.&lt;/P&gt;
&lt;P&gt;So the output data should have following column (number in parenthesis is value of each column for 1&lt;SUP&gt;st&lt;/SUP&gt; row which associated with above explanation) :&lt;/P&gt;
&lt;P&gt;ID T&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;series1_value&lt;/STRONG&gt;(500) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;SUM_series1&lt;/STRONG&gt; (1000)&lt;/P&gt;
&lt;P&gt;series2_value (3) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SUM_series2 (9)&lt;/P&gt;
&lt;P&gt;series3_value (-3) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SUM_series3 (-6)&lt;/P&gt;
&lt;P&gt;series4_value (-500)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SUM_series4 (-500)&lt;/P&gt;
&lt;P&gt;series5_value (500) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SUM_series5 (500)&lt;/P&gt;
&lt;P&gt;Can you please help me with that?&lt;/P&gt;
&lt;P&gt;Thank you so much.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID  T;
datalines;
1 0
1 500
1 500
1 0
1 3
1 0
1 0
1 3
1 3
1 -3
1 0
1 -3
1 -500
1 0
1 0
1 500
1 -5
1 -500
1 0
2 0
2 3
2 3
2 0
;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 29 Sep 2022 04:58:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-numbers-of-the-same-value-with-sequence/m-p/835767#M330435</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-09-29T04:58:16Z</dc:date>
    </item>
    <item>
      <title>Re: Count numbers of the same value with sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-numbers-of-the-same-value-with-sequence/m-p/835771#M330437</link>
      <description>&lt;P&gt;A few questions:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Do you want to do this for each 12 obs window in the data?&lt;/LI&gt;
&lt;LI&gt;Do you really want the value in the desired output to be&amp;nbsp;&lt;STRONG&gt;series1_value&lt;/STRONG&gt;&lt;SPAN&gt;(500)&amp;nbsp;? Why not just 500?&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Regards Peter.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 06:17:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-numbers-of-the-same-value-with-sequence/m-p/835771#M330437</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-09-29T06:17:19Z</dc:date>
    </item>
    <item>
      <title>Re: Count numbers of the same value with sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-numbers-of-the-same-value-with-sequence/m-p/835849#M330474</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I want to do it for all rows in the table.&lt;/P&gt;
&lt;P&gt;The column is &lt;STRONG&gt;series1_value&lt;/STRONG&gt;and the value in this column for the first row is &lt;STRONG&gt;500.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 14:21:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-numbers-of-the-same-value-with-sequence/m-p/835849#M330474</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-09-29T14:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: Count numbers of the same value with sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-numbers-of-the-same-value-with-sequence/m-p/835896#M330497</link>
      <description>&lt;P&gt;So I do nested loop to count the series of number (TT) with the same value.&lt;/P&gt;
&lt;P&gt;The basic idea is:&lt;/P&gt;
&lt;P&gt;Start with 1st row. Keep TT value in &lt;CODE class=" language-sas"&gt;value_series_1 &lt;/CODE&gt;.&lt;/P&gt;
&lt;P&gt;Loop to next row, if TT is the same as&amp;nbsp;&lt;CODE class=" language-sas"&gt;value_series_1 &lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;increase the&amp;nbsp;&lt;CODE class=" language-sas"&gt;N_series_1  &lt;/CODE&gt;by 1. If not the same, it is the start of &lt;CODE class=" language-sas"&gt;value_series_2 &lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;My code below work with lookback=12. It gives correct result for the first few rows but then at rows 8, number look weird with the N_series inflated to 30+.&lt;/P&gt;
&lt;P&gt;Same thing when lookback=4, the error show right on the first row.&lt;/P&gt;
&lt;P&gt;Look like the script doesn't stop at i+&amp;amp;lookback&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DO j=i+1 to i+&amp;amp;lookback until (End_run=1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I can't find the problem and really appreciate it if you could help.&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID TT;
datalines;
1 500
1 500
1 3
1 3
1 3
1 -3
1 -3
1 -500
1 500
1 -5
1 -500
1 3
1 3
;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;
%let lookback=5;


data want;
set have;
drop i End_run ID1-ID5 TT1-TT5;
i+1;
End_run=0;
N_series_1 = 0; value_series_1 =0;
N_series_2 = 0; value_series_2 =0;
N_series_3 = 0; value_series_3 =0;
N_series_4 = 0; value_series_4 =0;
N_series_5 = 0; value_series_5 =0;


N_series_1 = 1; value_series_1 =TT;
DO j=i+1 to i+&amp;amp;lookback until (End_run=1);

	set have(keep = ID TT rename =(ID=ID1 TT=TT1)) point=j nobs=nobs1;
	if TT1=value_series_1 then N_series_1=N_series_1+1; 
	else 
	if TT1^=value_series_1 then do; 
		N_series_2=N_series_2+1; value_series_2=TT1;
		DO k=j+1 to i+&amp;amp;lookback until (End_run=1);

		set have(keep = ID TT rename =(ID=ID2 TT=TT2)) point=k nobs=nobs2;
			if TT2=value_series_2 then N_series_2=N_series_2+1; 
			else 
			if TT2^=value_series_2 then do; 
				N_series_3=N_series_3+1; value_series_3=TT2;
				DO l=k+1 to i+&amp;amp;lookback until (End_run=1);

				set have(keep = ID TT rename =(ID=ID3 TT=TT3)) point=l nobs=nobs3;
					if TT3=value_series_3 then N_series_3=N_series_3+1; 
					else 
					if TT3^=value_series_3 then do; 
						N_series_4=N_series_4+1; value_series_4=TT3;
						DO m=l+1 to i+&amp;amp;lookback until (End_run=1);

						set have(keep = ID TT rename =(ID=ID4 TT=TT4)) point=m nobs=nobs4;
							if TT4=value_series_4 then N_series_4=N_series_4+1; 
							else 
							if TT4^=value_series_4 then do; 


						N_series_5=N_series_5+1; value_series_5=TT4;
						DO n=m+1 to i+&amp;amp;lookback until (End_run=1);

						set have(keep = ID TT rename =(ID=ID5 TT=TT5)) point=n nobs=nobs5;
							if TT5=value_series_5 then N_series_5=N_series_5+1; 
							else if TT5^=value_series_5 then do; 
							End_run=1;end;
END;END;END;END;;END;END;END;END;END;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 17:31:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-numbers-of-the-same-value-with-sequence/m-p/835896#M330497</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-09-29T17:31:15Z</dc:date>
    </item>
  </channel>
</rss>

