<?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 create a count before and after a particular case in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-before-and-after-a-particular-case/m-p/621318#M182651</link>
    <description>&lt;P&gt;Much easier in Datastep, But I wanted some fun In Proc SQL. Please wait for datastep solutions&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID    Count    Result ;*    Count_New;
cards;
1        1             0            2
1        2             0            2
1        3             1               
1        4             0            3
1        5             0            3
1        6             1            3 
2        1             0
2        2             0
3        1             1            0
4        1             0            2
4        2             0            2
4        3             1
5        1             1            0
5        2             0            2
5        3             1            2
;

proc sql;
create table want(drop=t:) as 
select *,count(t1) as Count_New 
from 
(select *,min(ifn(result=1,count,.)) as t,
case when count&amp;lt;calculated t then 1
when count&amp;gt; calculated t then 0 else . end as t1
from have
group by id)
group id ,t1
order by id,count;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 30 Jan 2020 22:05:21 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-01-30T22:05:21Z</dc:date>
    <item>
      <title>How to create a count before and after a particular case</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-before-and-after-a-particular-case/m-p/621314#M182648</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;I need to create a new count variable to find out number of tests before and after a first positive test. Not sure how to approach this situation. My data set contains ID, Count, Result (1/0, 1 for positive and 0 for negative). Same ID can have multiple observations.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; Count&amp;nbsp; &amp;nbsp; Result&amp;nbsp; &amp;nbsp; &amp;nbsp;Count_New&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jan 2020 21:35:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-before-and-after-a-particular-case/m-p/621314#M182648</guid>
      <dc:creator>sms1891</dc:creator>
      <dc:date>2020-01-30T21:35:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a count before and after a particular case</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-before-and-after-a-particular-case/m-p/621318#M182651</link>
      <description>&lt;P&gt;Much easier in Datastep, But I wanted some fun In Proc SQL. Please wait for datastep solutions&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID    Count    Result ;*    Count_New;
cards;
1        1             0            2
1        2             0            2
1        3             1               
1        4             0            3
1        5             0            3
1        6             1            3 
2        1             0
2        2             0
3        1             1            0
4        1             0            2
4        2             0            2
4        3             1
5        1             1            0
5        2             0            2
5        3             1            2
;

proc sql;
create table want(drop=t:) as 
select *,count(t1) as Count_New 
from 
(select *,min(ifn(result=1,count,.)) as t,
case when count&amp;lt;calculated t then 1
when count&amp;gt; calculated t then 0 else . end as t1
from have
group by id)
group id ,t1
order by id,count;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Jan 2020 22:05:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-before-and-after-a-particular-case/m-p/621318#M182651</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-30T22:05:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a count before and after a particular case</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-before-and-after-a-particular-case/m-p/621327#M182654</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID    Count    Result ;*    Count_New;
cards;
1        1             0            2
1        2             0            2
1        3             1               
1        4             0            3
1        5             0            3
1        6             1            3 
2        1             0
2        2             0
3        1             1            0
4        1             0            2
4        2             0            2
4        3             1
5        1             1            0
5        2             0            2
5        3             1            2
;

data want;
 do _n_=1 by 1 until(last.id);
  set have;
  by id;
  if t then continue;
  if result then t=count;
 end;
 _iorc_=count;
 do _n_=1 to _n_;
  set have;
  if t and count&amp;lt;t then Count_new=t-1;
  else if t and count&amp;gt;t then Count_new=_iorc_-t;
  else if t then Count_new=0;
  output;
 end;
 drop t;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Jan 2020 22:20:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-before-and-after-a-particular-case/m-p/621327#M182654</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-30T22:20:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a count before and after a particular case</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-before-and-after-a-particular-case/m-p/621359#M182664</link>
      <description>&lt;P&gt;I've modified your result data set a little bit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID    Count    Result ;*    Count_New;
cards;
1        1             0            2
1        2             0            2
1        3             1               
1        4             0            3
1        5             0            3
1        6             1            3
2        1             0
2        2             0
3        1             1            0
4        1             0            2
4        2             0            2
4        3             1
5        1             1            0
5        2             0            2
5        3             1            2
;

data want (drop=_:);
  do until (last.id);
    set have;
    by id;
    if result=1 and count_first_positive=. then count_first_positive=count;
  end;

  if count_first_positive^=. then count_new=count_first_positive-1;
  if count_first_positive^=. then _after_count=count-count_first_positive;

  do until (last.id);
    set have;
    by id;
    if count=count_first_positive then count_new=0;
    else if count_new=0 then count_new=_after_count;
    output;
  end;
run;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've added the variables COUNT_FIRST_POSITIVE to report the COUNT values for the first positive result.&amp;nbsp; This will allow you to look at any record in the resulting data set and know whether it precedes (COUNT&amp;lt;COUNT_FIRST_POSITIVE) or follows (COUNT&amp;gt;COUNT_FIRST_POSITIVE) the event. Otherwise when you look at COUNT_NEW you still don't know whether the current observations precedes or follows.&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jan 2020 02:59:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-before-and-after-a-particular-case/m-p/621359#M182664</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-01-31T02:59:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a count before and after a particular case</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-before-and-after-a-particular-case/m-p/621400#M182679</link>
      <description>&lt;P&gt;Maybe something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data want;
  set have;
  by id;
  if first.id then do;
    P_first=_N_;
    P_positive=.;
    end;
  if last.id then 
    P_last=_N_;
  if result=1 and P_positive=. then
    P_positive=_N_;
  retain P_:;
  drop P_:;
  if last.id;
  Count_New=P_positive-P_first;
  do _N_=P_first to P_positive-1;
    set have point=_N_;
    output;
    end;
  Count_New=.;
  set have point=P_positive;
  output;
  Count_New=P_last-P_positive;
  do _N_=P_positive+1 to P_last;
    set have point=_N_;
    output;
    end;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 Jan 2020 10:16:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-before-and-after-a-particular-case/m-p/621400#M182679</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-01-31T10:16:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a count before and after a particular case</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-before-and-after-a-particular-case/m-p/623653#M183641</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;Sorry for the late response. I have modified my request. I just want to identify tests that are done before 1st positive Result (i.e. all tests before 1st Result = 1) and all tests done after 1st result = 1 as two separate variables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for all the responses. I really appreciate it.&lt;/P&gt;&lt;P&gt;Here is what I need for the new count variable:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; Count&amp;nbsp; &amp;nbsp; Result&amp;nbsp; &amp;nbsp; &amp;nbsp;Test_Before&amp;nbsp; &amp;nbsp;Test_After&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Yes&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Yes&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Yes&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Yes&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2020 18:28:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-before-and-after-a-particular-case/m-p/623653#M183641</guid>
      <dc:creator>sms1891</dc:creator>
      <dc:date>2020-02-10T18:28:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a count before and after a particular case</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-before-and-after-a-particular-case/m-p/623656#M183644</link>
      <description>&lt;P&gt;Make a new retained variable to keep track of whether you have found a result yet.&lt;/P&gt;
&lt;P&gt;Then it is easy to get the logic to create your new variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id;
  retain found ;
  if first.id then found=0;
  if result and not found then do;
    found=1;
    test_before=0;
    test_after=0;
  end;
  else do;
    test_before=not found;
    test_after=found;
  end;
  drop found;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                 test_    test_
Obs    ID    Count    Result    before    after

  1     1      1         0         1        0
  2     1      2         0         1        0
  3     1      3         1         0        0
  4     1      4         0         0        1
  5     1      5         0         0        1
  6     1      6         1         0        1
  7     2      1         0         1        0
  8     2      2         0         1        0
  9     3      1         1         0        0
 10     4      1         0         1        0
 11     4      2         0         1        0
 12     4      3         1         0        0
 13     5      1         1         0        0
 14     5      2         0         0        1
 15     5      3         1         0        1&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Feb 2020 19:00:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-before-and-after-a-particular-case/m-p/623656#M183644</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-10T19:00:02Z</dc:date>
    </item>
  </channel>
</rss>

