<?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: How to count sequential values by observation in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591732#M15264</link>
    <description>&lt;P&gt;And to specifically answer the question in your scenario, I would want #seqneg to be 2.&lt;/P&gt;</description>
    <pubDate>Wed, 25 Sep 2019 23:40:22 GMT</pubDate>
    <dc:creator>aeb</dc:creator>
    <dc:date>2019-09-25T23:40:22Z</dc:date>
    <item>
      <title>How to count sequential values by observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591726#M15261</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset of laboratory results. Labs were collected at multiple time points. I am specifically interested in knowing if a patient tested negative at sequential time points. Specifically, if they tested negative twice or more in a row. Here's an example dataset, where neg1-neg5 is a yes/no (1/0) for a negative value. I do not want to reorder the dataset, as it is important to keep the results in the order in which they were received.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is an example of what I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;patid&lt;/TD&gt;&lt;TD&gt;neg1&lt;/TD&gt;&lt;TD&gt;neg2&lt;/TD&gt;&lt;TD&gt;neg3&lt;/TD&gt;&lt;TD&gt;neg4&lt;/TD&gt;&lt;TD&gt;neg5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I would like something like this, where #seqneg is the number of sequential negatives by patient.&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;patid&lt;/TD&gt;&lt;TD&gt;neg1&lt;/TD&gt;&lt;TD&gt;neg2&lt;/TD&gt;&lt;TD&gt;neg3&lt;/TD&gt;&lt;TD&gt;neg4&lt;/TD&gt;&lt;TD&gt;neg5&lt;/TD&gt;&lt;TD&gt;#seqneg&l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.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591726#M15261</guid>
      <dc:creator>aeb</dc:creator>
      <dc:date>2019-09-25T22:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to count sequential values by observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591728#M15262</link>
      <description>What about this case? &lt;BR /&gt;&lt;BR /&gt;patid neg1 neg2 neg3 neg4 neg5&lt;BR /&gt;6       1        1       0       1       1</description>
      <pubDate>Wed, 25 Sep 2019 23:05:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591728#M15262</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-25T23:05:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to count sequential values by observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591730#M15263</link>
      <description>Hi Reeza,&lt;BR /&gt;&lt;BR /&gt;You are actually anticipating my next step. At this point I'm only interested in any occurrence of two or more - but I will eventually want to know if they were interrupted by a positive result.&lt;BR /&gt;&lt;BR /&gt;I should also note, I have up to 10 results per patient (most patients do not have 10).&lt;BR /&gt;&lt;BR /&gt;Thank you!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 25 Sep 2019 23:24:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591730#M15263</guid>
      <dc:creator>aeb</dc:creator>
      <dc:date>2019-09-25T23:24:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to count sequential values by observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591732#M15264</link>
      <description>&lt;P&gt;And to specifically answer the question in your scenario, I would want #seqneg to be 2.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Sep 2019 23:40:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591732#M15264</guid>
      <dc:creator>aeb</dc:creator>
      <dc:date>2019-09-25T23:40:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to count sequential values by observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591739#M15266</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/178071"&gt;@aeb&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;'s intuitive reasoning is brilliant, so the following is solely for your sample. Not asking you to rely on this , but my share of fun&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
infile cards truncover;
input patid	neg1	neg2	neg3	neg4	neg5;
cards;
1	0	1	0	0	1
2	1	1	1	0	0
3	0	1	1	0	0
4	0	1	1	1	1
5	0	0	0	1	0
;

data want;
set have;
_k=cats(of neg:);
_f=findc(_k,'1');
_l=findc(_k,'1','b');
want= (countc(_k,'1')=_l-_f+1)*countc(_k,'1')*(_l-_f&amp;gt;0) ;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 00:17:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591739#M15266</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-26T00:17:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to count sequential values by observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591747#M15267</link>
      <description>&lt;P&gt;You apparently want the length of the longest sequence of '1', as long as that length is at least 2.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could start by converting&amp;nbsp;your sequence of numbers to a string (e.g. 0 1 1 1 0&amp;nbsp; becomes '01110').&amp;nbsp; You could then iteratively search STRNG for substrings&amp;nbsp;'11111', '1111', '111', '11', '1', '' until you have success.&amp;nbsp; Such a program would look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover;
input patid	neg1	neg2	neg3	neg4	neg5;
cards;
1	0	1	0	0	1
2	1	1	1	0	0
3	0	1	1	0	0
4	0	1	1	1	1
5	0	0	0	1	0
;

data want (drop=strng);
  set have;
  length strng $5;
  strng=cats(of neg:);

  do seqneg=5 to 1 by -1 until(find(strng,repeat('1',seqneg-1)));
  end;
  if  seqneg=1 then seqneg=0;
run;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "do seqneg=5 to 1 by -1 until (.....) stops when either the UNTIL condition resolves to a non-zero, or when&amp;nbsp;seqneg reaches 1.&amp;nbsp; The until is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;until(find(strng,repeat('1',seqneg-1)));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;which says search STRNG for the character "1" with SEQNEG-1 repetitions.&amp;nbsp;&amp;nbsp; If the FIND is unsuccessful, the function returns a zero, but otherwise returns the position in which the substring is found.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For a set of 10 NEG variables just change the length of STRNG to $10 and start the do loop at 10 instead of 5.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this looks a little complicated then you could change the code to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2 (drop=strng substrng found);
  set have;
  length strng $5  substrng $5;
  strng=cats(of neg:);
  do seqneg=5 to 1 by -1 until(found^=0);
    substrng=repeat('1',seqneg-1);
    found=find(strng,trim(substrng));
  end;
  if  seqneg=1 then seqneg=0;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which utilizes more intermediate variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 03:18:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591747#M15267</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-09-26T03:18:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to count sequential values by observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591755#M15270</link>
      <description>&lt;P&gt;Mark and novinosrin:&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;Thank you both, very much.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;novinosrin - you were right, code worked in specific scenarios but not all.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;Mark - worked and I appreciate the explanation.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;Thanks again!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 03:57:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591755#M15270</guid>
      <dc:creator>aeb</dc:creator>
      <dc:date>2019-09-26T03:57:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to count sequential values by observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591762#M15272</link>
      <description>&lt;P&gt;If you want to stick with numbers and auto adjust to any number of neg values:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover;
input patid neg1    neg2    neg3    neg4    neg5;
cards;
1   0   1   0   0   1
2   1   1   1   0   0
3   0   1   1   0   0
4   0   1   1   1   1
5   0   0   0   1   0
;

data want;
set have;
array a dum1 neg: dum2;
seq = 0;
do i = 2 to dim(a);
	if a{i} and not a{i-1} then start = i;
	else if a{i-1} and not a{i} then do;
		if i - 1 &amp;gt; start then
			if i - start &amp;gt; seq then seq = i - start;
		end;
	end;
drop dum1 dum2 i start;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Variables dum1 and dum2 pad the array and allow us to skip testing end conditions. /p&amp;gt;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 21:40:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591762#M15272</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-09-26T21:40:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to count sequential values by observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591912#M15294</link>
      <description>&lt;P&gt;An approach with transposition:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data c;
input PID neg1 neg2 neg3 neg4 neg5;
cards;
1 0 1 0 0 1
2 1 1 1 0 0
3 0 1 1 0 0
4 0 1 1 1 1
5 0 0 0 1 0
6 1 1 0 1 1
;
proc transpose data=c out=ct; var Neg: ; by PID;
run;

data cc; set ct; by PID;
	if first.PID=1									then count=0; else;
	if first.PID NE 1 AND Col1=1 AND lag(Col1)=1	then Count+1; else;
	if Col1=0										then Count=0;
run;

proc sql;
	select PID, max(Count) as Count
	from CC
	group by PID
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you skip the last line `IF COL1=0` it will continue the count: e.g., PID=6 will have the count of two rather than one.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 16:19:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591912#M15294</guid>
      <dc:creator>Haris</dc:creator>
      <dc:date>2019-09-26T16:19:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to count sequential values by observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591935#M15298</link>
      <description>&lt;P&gt;Thinking outside the box, you can coerce an input statement to read a concatenation of the flag variables transformed to appear like an csv input line.&amp;nbsp; Lots of tricks mashed into this answer:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;DATA Step with SET and INFILE&lt;/LI&gt;
&lt;LI&gt;Dummy DATALINES for INPUT to introduce _INFILE_&lt;/LI&gt;
&lt;LI&gt;INFILE CARDS with TRUNCOVER to prevent LOST CARD&lt;/LI&gt;
&lt;LI&gt;Held input with @@ to prevent dataline advancement&lt;/LI&gt;
&lt;LI&gt;INPUT @1 to reposition parse start point&lt;/LI&gt;
&lt;LI&gt;INPUT with (one:) variable list specifier to read into an array&lt;/LI&gt;
&lt;LI&gt;INPUT with (binary32.:) informat list to read 1's as binary data upto next delimiter (,) using : list input modifier&lt;/LI&gt;
&lt;LI&gt;MAX of one: to determine largest run of 1's&lt;/LI&gt;
&lt;LI&gt;LOG2 of 1+MAX to get number of 1's in largest value that must be 2^p - 1&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;* faux data with 32 neg flag variables;&lt;BR /&gt;&lt;BR /&gt;data have;
  do patid = 1 to 20;
    array neg neg1-neg32;
    do over neg;
      neg = ranuni(123) &amp;lt; 0.35;
    end;
    output;
  end;
run;

data want;
  attrib longest_run length=8;

  set have;
  infile cards dlm=',' truncover;

  if _n_ = 1 then input @1 @@; * activate _infile_;

  _infile_ = translate(cats(of neg:),',','0'); * repurpose _infile_ value;
  * put _infile_;

  array one one1-one32;
  input @1 (one:) (binary32.:) @@;  * read the _infile_ (input buffer);
  _error_ = 0;

  longest_run = log2 (1+max(of one:));

  drop one:;
cards;
.
run;

&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Sep 2019 17:51:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-sequential-values-by-observation/m-p/591935#M15298</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2019-09-26T17:51:26Z</dc:date>
    </item>
  </channel>
</rss>

