<?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 perform sum by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495589#M130815</link>
    <description>&lt;P&gt;Not 100% that this works, but it seems to provide the requested results:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  /* find all IDs having var1 and/or var2 missing */
   create table work.WithMissing as
      select distinct id
         from work.have2
            where missing(var1) or missing(var2)
   ;
  /* set coe1 to 1 for the IDs */
   update work.have2 
      set coe1 = 1 
         where id in (select id from work.WithMissing)
   ;
quit;

proc summary data=work.have2 nway;
   class id;
   var var1 var2;
   weight coe1;
   output out=work.want(drop= _type_ _freq_) sum=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 14 Sep 2018 06:58:41 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2018-09-14T06:58:41Z</dc:date>
    <item>
      <title>Conditional perform sum by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495567#M130800</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have question that how to conditional perform vertical sum as following:&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data have:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;VAR1&lt;/TD&gt;&lt;TD&gt;VAR2&lt;/TD&gt;&lt;TD&gt;COE1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;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t;P&gt;Data want:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;VAR1&lt;/TD&gt;&lt;TD&gt;VAR2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;2.5&lt;/TD&gt;&lt;TD&gt;4.5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;3.5&lt;/TD&gt;&lt;TD&gt;4.5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;where in data want,&lt;/P&gt;&lt;P&gt;Var1(A) = 2+1, Var2(A) =4+3;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Var1(B) =4*0.1+7*0.2+1*0.7=2.5&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;Var2(B) = 6*0.1+9*0.2+3*0.7=4.5;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Var1(C) = 5*0.5+2*0.5 = 3.5, Var2(C) = 6*0.5+3*0.5 = 4.5&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Sep 2018 04:29:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495567#M130800</guid>
      <dc:creator>lpy0521</dc:creator>
      <dc:date>2018-09-14T04:29:36Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional perform sum by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495573#M130805</link>
      <description>&lt;P&gt;here is a data step that gives your requested results&lt;/P&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 have;
input ID $ var1 var2 coe1;
cards;

A 2 4 0.2
A NA/. NA/. 0.3
A 1 3 0.5
B 4 6 0.1
B 7 9 0.2
B 1 3 0.7
C 5 6 0.5
C 2 3 0.5
;
proc sort data=have out=have2;
	by ID;
run;

data want(drop = var1_total var2_total coe1);
	set have2;
	by id;
	if first.ID then
		do;
			var1_total = 0;
			var2_total = 0;
		end;
	var1_total + var1;
	var2_total + var2;
	if last.ID then
		do;
			var1 = var1_total;
			var2 = var2_total;
			output;
		end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Sep 2018 04:17:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495573#M130805</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2018-09-14T04:17:48Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional perform sum by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495574#M130806</link>
      <description>&lt;P&gt;Thanks for your suggestion. But it gives the sum for B C, instead of the sum product. By sum product I mean:&lt;/P&gt;&lt;P&gt;Var1(B)*COE(B) + VAR2(B)*COE(B).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 14 Sep 2018 04:25:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495574#M130806</guid>
      <dc:creator>lpy0521</dc:creator>
      <dc:date>2018-09-14T04:25:44Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional perform sum by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495583#M130809</link>
      <description>&lt;P&gt;Lets solve the issue with the input data first: the string "NA/." can't be stored in a numeric variable. var1 and var2 must be numeric to be used in calculations. So please post the example data as data-step using the data-types you have in your original dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Except for the special treatment when var1 or var2 are missing in a group, this looks like weighted sums.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Sep 2018 06:03:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495583#M130809</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-09-14T06:03:36Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional perform sum by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495587#M130813</link>
      <description>&lt;P&gt;Sorry for the confusion here. What I mean by "NA/." is actually missing values and there are almost half of my var1 and var2 are missing. So basically I want to perform:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For each ID, if there is missing value in Var1 or Var2, then sum those non-missing value up, if there are no missing value, then calculate the weighted sum with corresponding COE1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your comment!&lt;/P&gt;</description>
      <pubDate>Fri, 14 Sep 2018 06:25:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495587#M130813</guid>
      <dc:creator>lpy0521</dc:creator>
      <dc:date>2018-09-14T06:25:00Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional perform sum by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495589#M130815</link>
      <description>&lt;P&gt;Not 100% that this works, but it seems to provide the requested results:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  /* find all IDs having var1 and/or var2 missing */
   create table work.WithMissing as
      select distinct id
         from work.have2
            where missing(var1) or missing(var2)
   ;
  /* set coe1 to 1 for the IDs */
   update work.have2 
      set coe1 = 1 
         where id in (select id from work.WithMissing)
   ;
quit;

proc summary data=work.have2 nway;
   class id;
   var var1 var2;
   weight coe1;
   output out=work.want(drop= _type_ _freq_) sum=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Sep 2018 06:58:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495589#M130815</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-09-14T06:58:41Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional perform sum by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495623#M130842</link>
      <description>&lt;P&gt;It can be done using SQL:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as select
    ID,
    case nmiss(var1)+nmiss(var2)
      when 0 then sum(var1*coe1)
      else sum(var1)
    end as var1,  
    case nmiss(var1)+nmiss(var2)
      when 0 then sum(var2*coe1)
      else sum(var2)
    end as var2
  from have
  group by id;
quit; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Or you can do it with datasteps like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data miss;
  set have(where=(var1 is null or var2 is null))
  by id;
  if first.id;
  keep id;
run;

data want;
  sum1=0;
  sum2=0;
  do until(last.id);
    merge miss(in=miss) have;
    by id; 
    if miss then do;
      sum1+var1;
      sum2+var2;
      end;
    else do; 
      sum1+var1*coe1;
      sum2+var2*coe1;
      end;
    end;
  drop sum1 sum2;
  var1=sum1;
  var2=sum2;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Sep 2018 10:41:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495623#M130842</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-09-14T10:41:24Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional perform sum by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495673#M130866</link>
      <description>&lt;P&gt;Assuming that your variables are actually numeric, and that your data is sorted ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not just compute both and track which one you want to use.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;var1_sum=0;&lt;/P&gt;
&lt;P&gt;var1_weighted=0;&lt;/P&gt;
&lt;P&gt;var2_sum=0;&lt;/P&gt;
&lt;P&gt;var2_weighted=0;&lt;/P&gt;
&lt;P&gt;do until (last.id);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;var1_sum + var1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;var2_sum + var2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if var1 &amp;gt; . then var1_weighted + var1*coe1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;else var1_missing=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if var2 &amp;gt; . then var2_weighted + var2*coe1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;else var2_missing=1;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;if var1_missing then var1=var1_sum;&lt;/P&gt;
&lt;P&gt;else var1= var1_weighted;&lt;/P&gt;
&lt;P&gt;if var2_missing then var2=var2_sum;&lt;/P&gt;
&lt;P&gt;else var2=var2_weighted;&lt;/P&gt;
&lt;P&gt;drop var1_: var2_: ;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Sep 2018 13:48:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-perform-sum-by-group/m-p/495673#M130866</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-09-14T13:48:47Z</dc:date>
    </item>
  </channel>
</rss>

