<?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 number of occurrence with sequence in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-occurrence-with-sequence/m-p/835943#M330518</link>
    <description>&lt;P&gt;It is exactly the idea.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to do for all rows and the loop stop when (lookback up reach 10 rows or series reach to 6, whatever come first.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
    <pubDate>Thu, 29 Sep 2022 20:19:53 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2022-09-29T20:19:53Z</dc:date>
    <item>
      <title>Count number of occurrence with sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-occurrence-with-sequence/m-p/835918#M330505</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;My data has ID and TT.&lt;/P&gt;
&lt;P&gt;For each row and within certain lookback window, I want to identify the number of time each number in TT show up.&lt;/P&gt;
&lt;P&gt;For the first row, we can see that the first set of TT value is 50 and it show up 2 time.&lt;/P&gt;
&lt;P&gt;After that, value 3 shows up for 2 times&lt;/P&gt;
&lt;P&gt;Then value -500 shows up for 1 time.&lt;/P&gt;
&lt;P&gt;I keep the value and the time in 2 set of variables: value_series_i and N_series_i&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code below use nested loop and it work correctly when I track series_1 and series_2.&lt;/P&gt;
&lt;P&gt;When I copy the code and add series_3, the results mess up. Somehow, it jump to last valid row and use this information for both series_2 and series_3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For reference purpose, for the first row:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;N_series_1: 2&lt;/P&gt;
&lt;P&gt;Value_series_1: 500&lt;/P&gt;
&lt;P&gt;N_series_2: 2&lt;/P&gt;
&lt;P&gt;Value_series_2: 3&lt;/P&gt;
&lt;P&gt;N_series_3: 1&lt;/P&gt;
&lt;P&gt;Value_series_3: -500&lt;/P&gt;
&lt;P&gt;I have spent several hours to clean the code and now truly have no clue of how to fix it and really appreciate 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 -500
1 -3

;run;

%let lookback=4;


*THIS CODE WORK CORRECTLY;
data want2;
set have nobs=nobs;
drop i k l End_run ID1-ID5 TT1-TT5 end_:;
i+1;
End_=0;
N_series_1 = 0; value_series_1 =0;

*series 1----;
N_series_1 = 1; value_series_1 =TT;
DO j=i+1 to min(nobs,i+&amp;amp;lookback) until (End_=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 do; 
*series 2 when value change---;
	N_series_2=1;value_series_2=TT1;
	DO k=j+1 to min(nobs,i+&amp;amp;lookback) until (End_=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 do; 
		End_=1;
		end;
END;END;END;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*THIS CODE DOESN'T WORK;
data want3;
set have nobs=nobs;
drop i k l End_run ID1-ID5 TT1-TT5 end_:;
i+1;
End_=0;
N_series_1 = 0; value_series_1 =0;

*series 1----;
N_series_1 = 1; value_series_1 =TT;
DO j=i+1 to min(nobs,i+&amp;amp;lookback) until (End_=1);
	set have(keep = ID TT rename =(ID=ID1 TT=TT1)) point=j;
	if TT1=value_series_1 then N_series_1=N_series_1+1;
	else do; 
*series 2 when value change---;
	N_series_2=1;value_series_2=TT1;
	DO k=j+1 to min(nobs,i+&amp;amp;lookback) until (End_=1);
		set have(keep = ID TT rename =(ID=ID2 TT=TT2)) point=k;
		if TT2=value_series_2 then N_series_2=N_series_2+1;	 
		else do; 
*series 3 when value change---;
		N_series_3=1;value_series_3=TT2;
		DO l=k+1 to min(nobs,i+&amp;amp;lookback) until (End_=1);
			set have(keep = ID TT rename =(ID=ID3 TT=TT3)) point=l;
			if TT3=value_series_3 then N_series_3=N_series_3+1;
			 
			else do; 
		*exit all;
			End_=1;end;

END;END;END;END;END;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 20:23:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-occurrence-with-sequence/m-p/835918#M330505</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-09-29T20:23:37Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of occurrence with sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-occurrence-with-sequence/m-p/835932#M330512</link>
      <description>&lt;P&gt;You didn't show us the expected result, but maybe this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
length var $16;
do i = 1 by 1 until(last.id);
    do n = 1 by 1 until(last.tt);
        set have; by id tt notsorted;
        end;
    var = cats("N_series_", i); value = n; output;
    var = cats("value_series_", i); value = tt; output;
    end;
keep id var value;
run;

proc transpose data=temp out=want(drop=_name_);
var value;
id var;
by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1664480464165.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75717i2D1B5EE609E3D02B/image-size/large?v=v2&amp;amp;px=999" role="button" title="PGStats_0-1664480464165.png" alt="PGStats_0-1664480464165.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 19:41:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-occurrence-with-sequence/m-p/835932#M330512</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2022-09-29T19:41:33Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of occurrence with sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-occurrence-with-sequence/m-p/835943#M330518</link>
      <description>&lt;P&gt;It is exactly the idea.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to do for all rows and the loop stop when (lookback up reach 10 rows or series reach to 6, whatever come first.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 20:19:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-occurrence-with-sequence/m-p/835943#M330518</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-09-29T20:19:53Z</dc:date>
    </item>
  </channel>
</rss>

