<?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: finding one event conditional on another variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/finding-one-event-conditional-on-another-variable/m-p/308030#M66029</link>
    <description>&lt;P&gt;My proposed solution is a two step process. The first part requires creating a temporary data set, &lt;EM&gt;&lt;STRONG&gt;work.tempdsn&lt;/STRONG&gt;&lt;/EM&gt;, containing a flag variable that sums the arf column by each id/date combination.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;proc sql;&lt;BR /&gt;create table work.tempdsn as &lt;BR /&gt;select distinct id&lt;BR /&gt;, date&lt;BR /&gt;, sum(arf) as flag&lt;BR /&gt;from work.dsn&lt;BR /&gt;group by id, date;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The idea being that any distinct id/date row containing a value for flag &amp;gt;=2&amp;nbsp;has arf=1 for the same date, so&amp;nbsp;arf2 needs to be set equal to 0 for that id. Below are the results from &lt;EM&gt;&lt;STRONG&gt;work.tempdsn&lt;/STRONG&gt;&lt;/EM&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;id      date      flag&lt;BR /&gt; 1    01/01/2005     2&lt;BR /&gt; 2    01/01/2005     1&lt;BR /&gt; 2    02/02/2005     0&lt;BR /&gt; 2    03/02/2005     1&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because the flag column equals 2 for the id 1 and date 01/01/2005 combination, arf=1 in the same date twice, meaning id #1 will receive an arf2 value of 0.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It gets a bit tricky with id #2, and all other id numbers with maximum flag amounts less than 2, because according to your stated requirements, in order for the arf2 column to be equal to 1 for a particular id, the original arf column must be equal to 1 for two different dates within that id. The HAVING statement in PROC SQL can accomodate this.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;create table work.outdsn as &lt;BR /&gt;select distinct id&lt;BR /&gt;, case when max(flag)&amp;gt;=2 then 0 &lt;BR /&gt; else 1 end as arf2&lt;BR /&gt;from work.tempdsn&lt;BR /&gt;group by id&lt;BR /&gt;having sum(flag)&amp;gt;=2;&lt;BR /&gt;drop table work.tempdsn;&lt;BR /&gt;quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If the maximum flag amount for an id is &amp;gt;=2, then arf2=0 for that id. If not, arf2=1. To ensure there are at least two different dates with arf=1, the having statement sums flag longitudinally across each id, keeping only the id numbers that have arf=1 for at least two dates within that id.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Since &lt;EM&gt;&lt;STRONG&gt;work.tempdsn&lt;/STRONG&gt;&lt;/EM&gt; is no longer needed, it is dropped from the WORK library, and the new data set, &lt;EM&gt;&lt;STRONG&gt;work.outdsn&lt;/STRONG&gt;&lt;/EM&gt;, is created containing the desired output below:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt;  arf2
&lt;SPAN class="token number"&gt; 1&lt;/SPAN&gt;     &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;
&lt;SPAN class="token number"&gt; 2&lt;/SPAN&gt;     &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only part I do not understand from your post is how to treat id variables were arf=1 in only one date and equal to 0 in all other dates. Since arf2 is binary, should these id numbers be set equal to 1? 0? Dropped from the final data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the code above, those id numbers are dropped from the final&amp;nbsp;data set. For example, the following data set will yield the same results in &lt;EM&gt;&lt;STRONG&gt;work.outdsn&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;because id #3 doesn't meet&amp;nbsp;either requirement of the arf2 variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;id      date      arf&lt;BR /&gt; 1    01/01/2005    1&lt;BR /&gt; 1    01/01/2005    1&lt;BR /&gt; 2    01/01/2005    1&lt;BR /&gt; 2    02/02/2005    0&lt;BR /&gt; 2    03/02/2005    1&lt;BR /&gt; 3    01/01/2005    1&lt;BR /&gt; 3    02/02/2005    0 &lt;BR /&gt; 3    03/02/2005    0&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 28 Oct 2016 21:40:25 GMT</pubDate>
    <dc:creator>jhlaramore</dc:creator>
    <dc:date>2016-10-28T21:40:25Z</dc:date>
    <item>
      <title>finding one event conditional on another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-one-event-conditional-on-another-variable/m-p/307953#M66010</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;id date arf       
1 01/01/2005 1
1 01/01/2005 1
2 01/01/2005 1
2 02/02/2005 0
2 03/02/2005 1&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi I am trying to create a variable conditional on the following:&lt;/P&gt;&lt;P&gt;if arf=1 in two different date then arf2= 1&lt;/P&gt;&lt;P&gt;if arf=1 in the same date arf2=0&lt;/P&gt;&lt;P&gt;for example for the above example, here is the output:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;id  arf2
1   0
2   1 &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Oct 2016 17:26:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-one-event-conditional-on-another-variable/m-p/307953#M66010</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2016-10-28T17:26:21Z</dc:date>
    </item>
    <item>
      <title>Re: finding one event conditional on another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-one-event-conditional-on-another-variable/m-p/308009#M66022</link>
      <description>&lt;P&gt;You may try next code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have; by id date; run; &amp;nbsp;/* if data is already sorted skip this line */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; by id date (where=(arf=1));&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;retain counter;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if first.id then counter =0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if first.date then counter +1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if last.id and counter GE 1 then arf2=1;&lt;/P&gt;
&lt;P&gt;&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else arf2=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; keep id arf2;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Oct 2016 19:57:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-one-event-conditional-on-another-variable/m-p/308009#M66022</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-10-28T19:57:42Z</dc:date>
    </item>
    <item>
      <title>Re: finding one event conditional on another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-one-event-conditional-on-another-variable/m-p/308030#M66029</link>
      <description>&lt;P&gt;My proposed solution is a two step process. The first part requires creating a temporary data set, &lt;EM&gt;&lt;STRONG&gt;work.tempdsn&lt;/STRONG&gt;&lt;/EM&gt;, containing a flag variable that sums the arf column by each id/date combination.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;proc sql;&lt;BR /&gt;create table work.tempdsn as &lt;BR /&gt;select distinct id&lt;BR /&gt;, date&lt;BR /&gt;, sum(arf) as flag&lt;BR /&gt;from work.dsn&lt;BR /&gt;group by id, date;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The idea being that any distinct id/date row containing a value for flag &amp;gt;=2&amp;nbsp;has arf=1 for the same date, so&amp;nbsp;arf2 needs to be set equal to 0 for that id. Below are the results from &lt;EM&gt;&lt;STRONG&gt;work.tempdsn&lt;/STRONG&gt;&lt;/EM&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;id      date      flag&lt;BR /&gt; 1    01/01/2005     2&lt;BR /&gt; 2    01/01/2005     1&lt;BR /&gt; 2    02/02/2005     0&lt;BR /&gt; 2    03/02/2005     1&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because the flag column equals 2 for the id 1 and date 01/01/2005 combination, arf=1 in the same date twice, meaning id #1 will receive an arf2 value of 0.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It gets a bit tricky with id #2, and all other id numbers with maximum flag amounts less than 2, because according to your stated requirements, in order for the arf2 column to be equal to 1 for a particular id, the original arf column must be equal to 1 for two different dates within that id. The HAVING statement in PROC SQL can accomodate this.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;create table work.outdsn as &lt;BR /&gt;select distinct id&lt;BR /&gt;, case when max(flag)&amp;gt;=2 then 0 &lt;BR /&gt; else 1 end as arf2&lt;BR /&gt;from work.tempdsn&lt;BR /&gt;group by id&lt;BR /&gt;having sum(flag)&amp;gt;=2;&lt;BR /&gt;drop table work.tempdsn;&lt;BR /&gt;quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If the maximum flag amount for an id is &amp;gt;=2, then arf2=0 for that id. If not, arf2=1. To ensure there are at least two different dates with arf=1, the having statement sums flag longitudinally across each id, keeping only the id numbers that have arf=1 for at least two dates within that id.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Since &lt;EM&gt;&lt;STRONG&gt;work.tempdsn&lt;/STRONG&gt;&lt;/EM&gt; is no longer needed, it is dropped from the WORK library, and the new data set, &lt;EM&gt;&lt;STRONG&gt;work.outdsn&lt;/STRONG&gt;&lt;/EM&gt;, is created containing the desired output below:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt;  arf2
&lt;SPAN class="token number"&gt; 1&lt;/SPAN&gt;     &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;
&lt;SPAN class="token number"&gt; 2&lt;/SPAN&gt;     &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only part I do not understand from your post is how to treat id variables were arf=1 in only one date and equal to 0 in all other dates. Since arf2 is binary, should these id numbers be set equal to 1? 0? Dropped from the final data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the code above, those id numbers are dropped from the final&amp;nbsp;data set. For example, the following data set will yield the same results in &lt;EM&gt;&lt;STRONG&gt;work.outdsn&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;because id #3 doesn't meet&amp;nbsp;either requirement of the arf2 variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;id      date      arf&lt;BR /&gt; 1    01/01/2005    1&lt;BR /&gt; 1    01/01/2005    1&lt;BR /&gt; 2    01/01/2005    1&lt;BR /&gt; 2    02/02/2005    0&lt;BR /&gt; 2    03/02/2005    1&lt;BR /&gt; 3    01/01/2005    1&lt;BR /&gt; 3    02/02/2005    0 &lt;BR /&gt; 3    03/02/2005    0&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Oct 2016 21:40:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-one-event-conditional-on-another-variable/m-p/308030#M66029</guid>
      <dc:creator>jhlaramore</dc:creator>
      <dc:date>2016-10-28T21:40:25Z</dc:date>
    </item>
    <item>
      <title>Re: finding one event conditional on another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-one-event-conditional-on-another-variable/m-p/308057#M66045</link>
      <description>&lt;PRE&gt;

data have;
input id date : mmddyy10. arf ;
format date mmddyy10.;
cards; 
1 01/01/2005 1
1 01/01/2005 1
2 01/01/2005 1
2 02/02/2005 0
2 03/02/2005 1
;
run;
proc sql;
 select id,case when(count(distinct date) ne 1) then 1 else 0 end as arf2
  from have
   where arf=1
    group by id;
quit;
&lt;/PRE&gt;</description>
      <pubDate>Sat, 29 Oct 2016 04:33:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-one-event-conditional-on-another-variable/m-p/308057#M66045</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-10-29T04:33:16Z</dc:date>
    </item>
    <item>
      <title>Re: finding one event conditional on another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-one-event-conditional-on-another-variable/m-p/308080#M66060</link>
      <description>&lt;P&gt;Thank you all for the reply!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Oct 2016 07:34:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-one-event-conditional-on-another-variable/m-p/308080#M66060</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2016-10-29T07:34:40Z</dc:date>
    </item>
  </channel>
</rss>

