<?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: Conditional sum in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988338#M43895</link>
    <description>&lt;P&gt;This is something that SQL syntax would make it easy to express.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You describe it as a SUM but to me it looks more like a COUNT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table want as 
select count(distinct&amp;nbsp;id) as n_ids_flagged 
  from&amp;nbsp;have&amp;nbsp;
  where&amp;nbsp;observation=1
    and id in (select id from have where flag='Y')
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 19 May 2026 19:55:14 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2026-05-19T19:55:14Z</dc:date>
    <item>
      <title>Conditional sum</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988320#M43892</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following dataset:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Flag :$20. Observation;
cards;
0001  Y  .
0001  N  1
0001  N  .
0001  N  1
0002  N  1
0002  N  1
0002  N  1
0002  N  .

...;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a way to sum all "Observation(s)" =1 but only for IDs having at least one Flag = "Y"?&lt;/P&gt;
&lt;P&gt;Desired output: sum = 1&lt;/P&gt;
&lt;P&gt;Thank you in advance&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2026 18:51:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988320#M43892</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-05-19T18:51:52Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional sum</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988321#M43893</link>
      <description>&lt;P&gt;Can you please show the output you want?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the same dataset, with a sum added, you could use a double-DOW loop, e.g.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
   do until(last.id) ;
     set db ;
     by id ;
     if flag='Y' then _flagged=1 ;
     _sum=sum(observation,_sum,0) ;
   end ;
   if _flagged then mysum=_sum ;
   do until(last.id) ;
     set db ;
     by id ;
     output ;
  end ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2026 19:01:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988321#M43893</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2026-05-19T19:01:39Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional sum</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988335#M43894</link>
      <description>Thank you very much for your help. I just need the sum stored in an output db since the sum should refer to all IDs with at least one value of "Flag = Y" and not in particular to one or another ID.</description>
      <pubDate>Tue, 19 May 2026 19:36:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988335#M43894</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-05-19T19:36:01Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional sum</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988338#M43895</link>
      <description>&lt;P&gt;This is something that SQL syntax would make it easy to express.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You describe it as a SUM but to me it looks more like a COUNT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table want as 
select count(distinct&amp;nbsp;id) as n_ids_flagged 
  from&amp;nbsp;have&amp;nbsp;
  where&amp;nbsp;observation=1
    and id in (select id from have where flag='Y')
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2026 19:55:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988338#M43895</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-05-19T19:55:14Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional sum</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988339#M43896</link>
      <description>&lt;P&gt;From your example data, shouldn't the answer be 2 rather than 1?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could still use a DOW-loop approach to flag the IDs of interest, then do the summing of the flagged records:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=mysum);
   do until(last.id) ;
     set db ;
     by id ;
     if flag='Y' then _flagged=1 ;
   end ;
   do until(last.id) ;
     set db end=last ;
     by id ;
     if _flagged then mysum++observation;
  end ;
  if last then output ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or via SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  create table want as 
  select sum(observation) as mysum
  from
    (select id, observation
      from db
      group by id
      having max(flag)='Y'
    )
 ;
quit ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2026 19:51:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988339#M43896</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2026-05-19T19:51:05Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional sum</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988348#M43897</link>
      <description>&lt;P&gt;I think it can be done with one data pass:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Flag :$20. Observation;
cards;
0001  Y  .
0001  N  1
0001  N  .
0001  N  1
0002  N  1
0002  N  1
0002  N  1
0002  N  .
0003  N  .
0003  N  1
0003  Y  .
0003  N  1
;
run;

data want;
  set db end=_E_;
  by id;
  if first.id then do; s=0; check=0; end;
  s + (Observation=1);
  check + (Flag="Y");
  if last.id and check then sum+s;
  if _E_;
run;
proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;B.&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2026 20:38:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988348#M43897</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2026-05-19T20:38:22Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional sum</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988349#M43898</link>
      <description>&lt;P&gt;I think even almost "unconditionall" &lt;SPAN class="lia-emoticons-autosuggestions"&gt;&lt;SPAN class="lia-emoticons-search-term"&gt;&lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
  set db end=_E_;
  by id;
  
  s=s*(^first.id); 
  check=check*(^first.id);

  s + (Observation=1);
  check + (Flag="Y");
  sum+s*(last.id*check);

  if _E_;
run;
proc print data=want2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN class="lia-emoticons-autosuggestions"&gt;&lt;SPAN class="lia-emoticons-search-term"&gt;B&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2026 20:46:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988349#M43898</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2026-05-19T20:46:26Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional sum</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988351#M43899</link>
      <description>&lt;P&gt;I like it,&amp;nbsp; Bart&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since OP hints that there could be more than one record for an ID with flag='Y', perhaps:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;check + (Flag="Y");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Should be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;check + ((Flag="Y") and (Not check));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2026 21:11:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988351#M43899</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2026-05-19T21:11:32Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional sum</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988365#M43900</link>
      <description>&lt;P&gt;in this case we can do it like&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;^^check&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;check&amp;amp;1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;{EDIT:} or even just&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;amp; check&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
  set db end=_E_;
  by id;
  
  s=s*(^first.id); 
  check=check*(^first.id);

  s + (Observation=1);
  check + (Flag="Y");
  sum+s*(last.id*^^check);
  /* sum+s*(last.id*check&amp;amp;1); */
  /* sum+s*(last.id &amp;amp; check); */

  if _E_;
run;
proc print data=want2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2026 09:17:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Conditional-sum/m-p/988365#M43900</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2026-05-20T09:17:44Z</dc:date>
    </item>
  </channel>
</rss>

