<?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: flag and count by id when condition is met in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/flag-and-count-by-id-when-condition-is-met/m-p/646497#M193413</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover expandtabs ;
input id vis test  loc $ ; /*  countL   countR */
n+1;
cards;
1	0	.		        
1	1	1	Left      	 
1	1	1	Right      	 
1	2	1	Left      	 
1	2	0	Right      	 
1	3	0	Left      	 
1	3	0	Right      	 
1	4	1	Left      	 
1	5	1	Left      	 
1	6	0		         
1	7	0		         
1	8	1	Left      	 
1	8	1	Left      	 
1	8	1	Right      	 
1	9	0		         
1	9	0	Right      	 
1	10	0		         
1	11	1	Left      	 
1	12	1	Left      	 
1	13	0		         
1	13	1	Right      	 
1	14	0		         
1	14	1	Right      	 
1	15	0	Left      	 
1	15	0	Right      	 
1	16	0	Left      	 
2	0			         
2	1	1	Left      	 
2	2	1	Left      	 
2	3	1	Left      	 
2	3	1	Right      	 
2	4	0		         
2	4	1	Right      	 
2	4	1	Right      	 
2	5	0		         
2	6	1	Left      	 
2	6	1	Right      	 
2	7	1	Left      	 
2	7	1	Right      	 
2	8	0	Left      	 
2	8	0	Right      	 
2	9	1	Left      	 
2	10	1	Left      	 
2	11	1	Left      	 
2	12	1	Left      	 
2	12	1	Right      	 
2	13	0	Left      	 
2	13	1	Right      	 
2	14	0	Left      	 
2	14	0	Right      	 
2	15	0	Left      	 
2	16	0	Left      	 
2	17	1	Left      	 
2	18	0	Left      	 
2	18	1	Right       
2	19	1	Left      	 
2	19	0	Right      	 
2	20	1	Left      	 
2	20	0	Right      	 
2	21	1	Left      	 
2	21	0	Right       
2	22	0	Left      	 
2	22	1	Right      	 
2	23	0		         
2	24	0	Left      	 
2	24	1	Right      	 
2	25	0	Left      	 
2	25	1	Right      	 
; run; 
data test_0 test_1;
 set have;
 if test=1 then output test_1;
  else output test_0;
run;
data temp;
 set test_0;
 by id;
 if first.id then do;_cl=0;_cr=0;end;
 cl=0;cr=0;
 if loc='Left' then do;_cl+1;cl=_cl;end;
 if loc='Right' then do;_cr+1;cr=_cr;end;
 drop _:;
run;
data want;
 set temp test_1;
run;
proc sort data=want;by n;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 10 May 2020 11:46:06 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2020-05-10T11:46:06Z</dc:date>
    <item>
      <title>flag and count by id when condition is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flag-and-count-by-id-when-condition-is-met/m-p/646400#M193349</link>
      <description>&lt;P&gt;&lt;CODE class=" language-sas"&gt;
&lt;/CODE&gt;&lt;FONT face="times new roman,times" size="4"&gt;Dear Everyone,&lt;BR /&gt;Can I please seek your help on how do I flag and count by id and loc when test=0 is met to get the last 2 columns -&amp;nbsp;countL and countR?&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="times new roman,times" size="4"&gt;Thanking you in advance.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;id vis test  loc   countL   countR
1	0			        0	0
1	1	1	Left      	0	0
1	1	1	Right      	0	0
1	2	1	Left      	0	0
1	2	0	Right      	0	1
1	3	0	Left      	1	0
1	3	0	Right      	0	2
1	4	1	Left      	0	0
1	5	1	Left      	0	0
1	6	0		        2	3
1	7	0		        3	4
1	8	1	Left      	0	0
1	8	1	Left      	0	0
1	8	1	Right      	0	0
1	9	0		        4	5
1	9	0	Right      	0	6
1	10	0		        5	7
1	11	1	Left      	0	0
1	12	1	Left      	0	0
1	13	0		        6	8
1	13	1	Right      	0	0
1	14	0		        7	9
1	14	1	Right      	0	0
1	15	0	Left      	8	0
1	15	0	Right      	0	10
1	16	0	Left      	9	0
2	0			        0	0
2	1	1	Left      	0	0
2	2	1	Left      	0	0
2	3	1	Left      	0	0
2	3	1	Right      	0	0
2	4	0		        1	1
2	4	1	Right      	0	0
2	4	1	Right      	0	0
2	5	0		        2	2
2	6	1	Left      	0	0
2	6	1	Right      	0	0
2	7	1	Left      	0	0
2	7	1	Right      	0	0
2	8	0	Left      	3	0
2	8	0	Right      	0	3
2	9	1	Left      	0	0
2	10	1	Left      	0	0
2	11	1	Left      	0	0
2	12	1	Left      	0	0
2	12	1	Right      	0	0
2	13	0	Left      	4	4
2	13	1	Right      	0	0
2	14	0	Left      	5	0
2	14	0	Right      	0	4
2	15	0	Left      	6	0
2	16	0	Left      	7	0
2	17	1	Left      	0	0
2	18	0	Left      	8	0
2	18	1	Right      	0	0
2	19	1	Left      	0	0
2	19	0	Right      	0	5
2	20	1	Left      	0	0
2	20	0	Right      	0	6
2	21	1	Left      	0	0
2	21	0	Right      	0	7
2	22	0	Left      	9	9
2	22	1	Right      	0	0
2	23	0		        10	8
2	24	0	Left      	11	0
2	24	1	Right      	0	0
2	25	0	Left      	12	0
2	25	1	Right      	0	0
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 May 2020 16:37:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flag-and-count-by-id-when-condition-is-met/m-p/646400#M193349</guid>
      <dc:creator>Miracle</dc:creator>
      <dc:date>2020-05-09T16:37:15Z</dc:date>
    </item>
    <item>
      <title>Re: flag and count by id when condition is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flag-and-count-by-id-when-condition-is-met/m-p/646401#M193350</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16719"&gt;@Miracle&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please specify the desired output?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Sat, 09 May 2020 15:54:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flag-and-count-by-id-when-condition-is-met/m-p/646401#M193350</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-05-09T15:54:28Z</dc:date>
    </item>
    <item>
      <title>Re: flag and count by id when condition is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flag-and-count-by-id-when-condition-is-met/m-p/646406#M193351</link>
      <description>&lt;P&gt;&lt;FONT face="times new roman,times"&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;&amp;nbsp; thanks for your reply.&amp;nbsp;&lt;BR /&gt;Based on the first four columns data, I would like to add two new columns - countL and countR.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="times new roman,times"&gt;Thanks in advance.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 09 May 2020 16:35:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flag-and-count-by-id-when-condition-is-met/m-p/646406#M193351</guid>
      <dc:creator>Miracle</dc:creator>
      <dc:date>2020-05-09T16:35:02Z</dc:date>
    </item>
    <item>
      <title>Re: flag and count by id when condition is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flag-and-count-by-id-when-condition-is-met/m-p/646413#M193357</link>
      <description>&lt;P&gt;It seems to me that next code result with what you want:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
retain id vis test loc;
infile cards truncover dlm='09'x;
length test $5;
input id vis test  loc $ ; /*  countL   countR */
cards;
1	0	.		        
1	1	1	Left      	 
1	1	1	Right      	 
1	2	1	Left      	 
1	2	0	Right      	 
1	3	0	Left      	 
1	3	0	Right      	 
1	4	1	Left      	 
1	5	1	Left      	 
1	6	0		         
1	7	0		         
1	8	1	Left      	 
1	8	1	Left      	 
1	8	1	Right      	 
1	9	0		         
1	9	0	Right      	 
1	10	0		         
1	11	1	Left      	 
1	12	1	Left      	 
1	13	0		         
1	13	1	Right      	 
1	14	0		         
1	14	1	Right      	 
1	15	0	Left      	 
1	15	0	Right      	 
1	16	0	Left      	 
2	0			         
2	1	1	Left      	 
2	2	1	Left      	 
2	3	1	Left      	 
2	3	1	Right      	 
2	4	0		         
2	4	1	Right      	 
2	4	1	Right      	 
2	5	0		         
2	6	1	Left      	 
2	6	1	Right      	 
2	7	1	Left      	 
2	7	1	Right      	 
2	8	0	Left      	 
2	8	0	Right      	 
2	9	1	Left      	 
2	10	1	Left      	 
2	11	1	Left      	 
2	12	1	Left      	 
2	12	1	Right      	 
2	13	0	Left      	 
2	13	1	Right      	 
2	14	0	Left      	 
2	14	0	Right      	 
2	15	0	Left      	 
2	16	0	Left      	 
2	17	1	Left      	 
2	18	0	Left      	 
2	18	1	Right       
2	19	1	Left      	 
2	19	0	Right      	 
2	20	1	Left      	 
2	20	0	Right      	 
2	21	1	Left      	 
2	21	0	Right       
2	22	0	Left      	 
2	22	1	Right      	 
2	23	0		         
2	24	0	Left      	 
2	24	1	Right      	 
2	25	0	Left      	 
2	25	1	Right      	 
; run; 


data want;
 set have;
  by id;
     retain cL cR 0;
  if first.id then do;
     cL=0; cR=0;
  end;
  if test=0 then do;   
     if loc='Left' then cL+1; else
     if loc='Right' then cR+1; else 
     do; cL+1; cR+1; end;
  end;
  if test=0  then do;
     if missing(loc) then 
          do; countL=cL; countR=cR; end; 
     else do;
          if loc='Left' then countL = cL; else countL=0;
          if loc='Right' then countR = cR; else countR=0;
     end;
  end;
  else do; countL=0; countR=0; end;
  drop cL cR;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 09 May 2020 18:33:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flag-and-count-by-id-when-condition-is-met/m-p/646413#M193357</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-05-09T18:33:34Z</dc:date>
    </item>
    <item>
      <title>Re: flag and count by id when condition is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flag-and-count-by-id-when-condition-is-met/m-p/646497#M193413</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover expandtabs ;
input id vis test  loc $ ; /*  countL   countR */
n+1;
cards;
1	0	.		        
1	1	1	Left      	 
1	1	1	Right      	 
1	2	1	Left      	 
1	2	0	Right      	 
1	3	0	Left      	 
1	3	0	Right      	 
1	4	1	Left      	 
1	5	1	Left      	 
1	6	0		         
1	7	0		         
1	8	1	Left      	 
1	8	1	Left      	 
1	8	1	Right      	 
1	9	0		         
1	9	0	Right      	 
1	10	0		         
1	11	1	Left      	 
1	12	1	Left      	 
1	13	0		         
1	13	1	Right      	 
1	14	0		         
1	14	1	Right      	 
1	15	0	Left      	 
1	15	0	Right      	 
1	16	0	Left      	 
2	0			         
2	1	1	Left      	 
2	2	1	Left      	 
2	3	1	Left      	 
2	3	1	Right      	 
2	4	0		         
2	4	1	Right      	 
2	4	1	Right      	 
2	5	0		         
2	6	1	Left      	 
2	6	1	Right      	 
2	7	1	Left      	 
2	7	1	Right      	 
2	8	0	Left      	 
2	8	0	Right      	 
2	9	1	Left      	 
2	10	1	Left      	 
2	11	1	Left      	 
2	12	1	Left      	 
2	12	1	Right      	 
2	13	0	Left      	 
2	13	1	Right      	 
2	14	0	Left      	 
2	14	0	Right      	 
2	15	0	Left      	 
2	16	0	Left      	 
2	17	1	Left      	 
2	18	0	Left      	 
2	18	1	Right       
2	19	1	Left      	 
2	19	0	Right      	 
2	20	1	Left      	 
2	20	0	Right      	 
2	21	1	Left      	 
2	21	0	Right       
2	22	0	Left      	 
2	22	1	Right      	 
2	23	0		         
2	24	0	Left      	 
2	24	1	Right      	 
2	25	0	Left      	 
2	25	1	Right      	 
; run; 
data test_0 test_1;
 set have;
 if test=1 then output test_1;
  else output test_0;
run;
data temp;
 set test_0;
 by id;
 if first.id then do;_cl=0;_cr=0;end;
 cl=0;cr=0;
 if loc='Left' then do;_cl+1;cl=_cl;end;
 if loc='Right' then do;_cr+1;cr=_cr;end;
 drop _:;
run;
data want;
 set temp test_1;
run;
proc sort data=want;by n;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 10 May 2020 11:46:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flag-and-count-by-id-when-condition-is-met/m-p/646497#M193413</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-05-10T11:46:06Z</dc:date>
    </item>
    <item>
      <title>Re: flag and count by id when condition is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flag-and-count-by-id-when-condition-is-met/m-p/646511#M193427</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;, not quite but thanks.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Again thanks to you both&amp;nbsp;&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":hugging_face:"&gt;🤗&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 10 May 2020 14:57:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flag-and-count-by-id-when-condition-is-met/m-p/646511#M193427</guid>
      <dc:creator>Miracle</dc:creator>
      <dc:date>2020-05-10T14:57:35Z</dc:date>
    </item>
  </channel>
</rss>

