<?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 0's until next 1 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-0-s-until-next-1/m-p/761982#M241200</link>
    <description>&lt;P&gt;Your code doesn't run. Please always vet it by pasting it back in SAS.&lt;/P&gt;
&lt;P&gt;This does your count:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  set HAVE;
  by ID;
  if first.ID then call missing(START,COUNT);
  if SHOCK=1 then START+1;
  if START &amp;amp; ^STATUS then COUNT+1;
  if STATUS then START=0;
  if last.ID;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 17 Aug 2021 06:58:59 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2021-08-17T06:58:59Z</dc:date>
    <item>
      <title>Count number of 0's until next 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-0-s-until-next-1/m-p/761971#M241190</link>
      <description>&lt;P&gt;I have dataset similar to the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
input id	day	shock	status

;
DATALINES;
1001	33	.	1
1001	34	1	0
1001	35	.	0
1001	36	.	0
1001	37	.	0
1001	38	.	1
1001	39	.	1
1001	40	.	1
1001	41	.	0
1001	42	.	0
1005	55	.	1
1005	56	.	1
1005	57	.	1
1005	58	.	1
1005	59	1	0
1005	60	.	1
1005	61	.	1


;
RUN;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;For each id, I want to count the number of times that status takes on a value of zero starting from the day where shock=1 until the next time that status=1. Note that in the data, when shock=1, status always takes on a value of 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The resultant data should be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want;
input id	count
;
DATALINES;
1001	4
1005	1
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;For example, for id=1001, when shock=1 on day 34, status=0 until day 38 when status becomes 1. There are 4 days on which status=0, so count=4 in the "want" dataset. Note that on day 41, status also equals to 0 but there is no value of 1 for shock, so these zeros are not counted.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Aug 2021 06:47:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-0-s-until-next-1/m-p/761971#M241190</guid>
      <dc:creator>elbarto</dc:creator>
      <dc:date>2021-08-17T06:47:47Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of 0's until next 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-0-s-until-next-1/m-p/761977#M241195</link>
      <description>&lt;P&gt;Why is COUNT=1 for ID=1005?&lt;/P&gt;</description>
      <pubDate>Tue, 17 Aug 2021 06:37:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-0-s-until-next-1/m-p/761977#M241195</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-08-17T06:37:07Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of 0's until next 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-0-s-until-next-1/m-p/761980#M241198</link>
      <description>Opps, I had made a typo, I fixed it now.</description>
      <pubDate>Tue, 17 Aug 2021 06:48:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-0-s-until-next-1/m-p/761980#M241198</guid>
      <dc:creator>elbarto</dc:creator>
      <dc:date>2021-08-17T06:48:01Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of 0's until next 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-0-s-until-next-1/m-p/761982#M241200</link>
      <description>&lt;P&gt;Your code doesn't run. Please always vet it by pasting it back in SAS.&lt;/P&gt;
&lt;P&gt;This does your count:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  set HAVE;
  by ID;
  if first.ID then call missing(START,COUNT);
  if SHOCK=1 then START+1;
  if START &amp;amp; ^STATUS then COUNT+1;
  if STATUS then START=0;
  if last.ID;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Aug 2021 06:58:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-0-s-until-next-1/m-p/761982#M241200</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-08-17T06:58:59Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of 0's until next 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-0-s-until-next-1/m-p/762035#M241222</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
input id	day	shock	status;
DATALINES;
1001	33	.	1
1001	34	1	0
1001	35	.	0
1001	36	.	0
1001	37	.	0
1001	38	.	1
1001	39	.	1
1001	40	.	1
1001	41	.	0
1001	42	.	0
1005	55	.	1
1005	56	.	1
1005	57	.	1
1005	58	.	1
1005	59	1	0
1005	60	.	1
1005	61	.	1
;
RUN;
data temp;
 set have;
 by id;
 if first.id or shock=1 or status ne lag(status) then group+1;
run;
proc sql;
create table want as
select id ,sum(status=0) as count
 from temp
  where group in (select group from temp where shock=1)
   group by id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Aug 2021 12:48:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-0-s-until-next-1/m-p/762035#M241222</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-08-17T12:48:45Z</dc:date>
    </item>
  </channel>
</rss>

