<?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: Using formulas to sum values between rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873285#M345031</link>
    <description>&lt;P&gt;This is just one formula I have in a large dataset where the formula variable includes like 50 different formulas. I just made this table to simplify my question. I have SAS correctly referencing all of the other formulas using call execute but this one is giving me trouble. I want SAS to reference the correct value. "&lt;SPAN&gt;201820_20:MemberCount" is referencing the value of MemberCount for the IndicatorKey=201820_20 which would be 323 (the first row) and so on and so forth. So the final column Calc should just add up each row to 1800 (323+560+257+660).&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 01 May 2023 22:42:57 GMT</pubDate>
    <dc:creator>linaibrou</dc:creator>
    <dc:date>2023-05-01T22:42:57Z</dc:date>
    <item>
      <title>Using formulas to sum values between rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873283#M345029</link>
      <description>&lt;P&gt;I have a dataset below. The formula is just summing each value in the MemberCount column. I thought about just creating a new variable for each value but is there an easier way to do this? Thanks!&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;IndicatorKey&lt;/TD&gt;&lt;TD&gt;MemberCount&lt;/TD&gt;&lt;TD&gt;Formula&lt;/TD&gt;&lt;TD&gt;Calc&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;201820_20&lt;/TD&gt;&lt;TD&gt;323&lt;/TD&gt;&lt;TD&gt;(201820_20:MemberCount+201822_20:MemberCount+201824_20:MemberCount+201828_20:MemberCount)&lt;/TD&gt;&lt;TD&gt;1800&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;201822_20&lt;/TD&gt;&lt;TD&gt;560&lt;/TD&gt;&lt;TD&gt;(201820_20:MemberCount+201822_20:MemberCount+201824_20:MemberCount+201828_20:MemberCount)&lt;/TD&gt;&lt;TD&gt;1800&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;201824_20&lt;/TD&gt;&lt;TD&gt;257&lt;/TD&gt;&lt;TD&gt;(201820_20:MemberCount+201822_20:MemberCount+201824_20:MemberCount+201828_20:MemberCount)&lt;/TD&gt;&lt;TD&gt;1800&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;201828_20&lt;/TD&gt;&lt;TD&gt;660&lt;/TD&gt;&lt;TD&gt;(201820_20:MemberCount+201822_20:MemberCount+201824_20:MemberCount+201828_20:MemberCount)&lt;/TD&gt;&lt;TD&gt;1800&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 01 May 2023 22:18:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873283#M345029</guid>
      <dc:creator>linaibrou</dc:creator>
      <dc:date>2023-05-01T22:18:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using formulas to sum values between rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873284#M345030</link>
      <description>&lt;P&gt;Your formulas all appear to be identical, so I see no reason to parse the formula row by row instead of making a new variable with the single, repeated, formula in your program code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But also, I do not understand your formula, which seems to be 5 colon-separated expressions. How would they be interpreted?&lt;/P&gt;</description>
      <pubDate>Mon, 01 May 2023 22:26:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873284#M345030</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-05-01T22:26:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using formulas to sum values between rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873285#M345031</link>
      <description>&lt;P&gt;This is just one formula I have in a large dataset where the formula variable includes like 50 different formulas. I just made this table to simplify my question. I have SAS correctly referencing all of the other formulas using call execute but this one is giving me trouble. I want SAS to reference the correct value. "&lt;SPAN&gt;201820_20:MemberCount" is referencing the value of MemberCount for the IndicatorKey=201820_20 which would be 323 (the first row) and so on and so forth. So the final column Calc should just add up each row to 1800 (323+560+257+660).&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 01 May 2023 22:42:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873285#M345031</guid>
      <dc:creator>linaibrou</dc:creator>
      <dc:date>2023-05-01T22:42:57Z</dc:date>
    </item>
    <item>
      <title>Re: Using formulas to sum values between rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873292#M345034</link>
      <description>&lt;P&gt;If you want to aggregate by the value of variable like INDICATORKEY then just tell SAS to do that directly.&amp;nbsp; No need for a "formula".&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means ;
   class indicatorkey;
   var membercount;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 May 2023 02:13:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873292#M345034</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-02T02:13:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using formulas to sum values between rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873293#M345035</link>
      <description>&lt;P&gt;If the example you provide is representative, then you want the sum of calc over all obs that have the same formula.&amp;nbsp; And for each distinct formula, the summation includes all the corresponding calc values and nothing else.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if the data are already physically grouped by formula, then this would work:&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.formula);
    set have;
    by formula notsorted;
    calc=sum(calc,membercount);
  end;
  do until (last.formula);
    set have;
    by formula notsorted;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now this does create a new variable, but it's a trivial effort.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 02:28:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873293#M345035</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-05-02T02:28:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using formulas to sum values between rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873299#M345040</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input IndicatorKey :$20.	MemberCount	Formula	 $100.;
cards;
201820_20	323	(201820_20:MemberCount+201822_20:MemberCount+201824_20:MemberCount+201828_20:MemberCount)	
201822_20	560	(201820_20:MemberCount+201822_20:MemberCount+201824_20:MemberCount+201828_20:MemberCount)	
201824_20	257	(201820_20:MemberCount+201822_20:MemberCount+201824_20:MemberCount+201828_20:MemberCount)	
201828_20	660	(201820_20:MemberCount+201822_20:MemberCount+201824_20:MemberCount+201828_20:MemberCount)	
;
data want;
 if _n_=1 then do;
   if 0 then set have(rename=(MemberCount=_MemberCount));
   declare hash h(dataset:'have(rename=(MemberCount=_MemberCount))');
   h.definekey('IndicatorKey');
   h.definedata('_MemberCount');
   h.definedone();
 end;
set have;
want=0;
do i=1 to countw(Formula,'_','kd');
 if h.find(key:scan(Formula,i,'_','kd'))=0 then want+_MemberCount;
end;
drop i _MemberCount;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 May 2023 04:16:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873299#M345040</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-05-02T04:16:45Z</dc:date>
    </item>
    <item>
      <title>Re: Using formulas to sum values between rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873422#M345103</link>
      <description>Thank you! This worked!</description>
      <pubDate>Tue, 02 May 2023 16:09:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873422#M345103</guid>
      <dc:creator>linaibrou</dc:creator>
      <dc:date>2023-05-02T16:09:29Z</dc:date>
    </item>
    <item>
      <title>Re: Using formulas to sum values between rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873424#M345105</link>
      <description>&lt;P&gt;Yikes.&amp;nbsp; What a complicated process for such a simple task.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql  ;
create table want2 as
  select *
       , sum(membercount) as want
  from have
  group by formula
  order by 1
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is result of comparing WANT to WANT2.&lt;/P&gt;
&lt;PRE&gt;Observation Summary                                                                                                                 
                                                                                                                                    
Observation      Base  Compare                                                                                                      
                                                                                                                                    
First Obs           1        1                                                                                                      
Last  Obs           4        4                                                                                                      
                                                                                                                                    
Number of Observations in Common: 4.                                                                                                
Total Number of Observations Read from WORK.WANT: 4.                                                                                
Total Number of Observations Read from WORK.WANT2: 4.                                                                               
                                                                                                                                    
Number of Observations with Some Compared Variables Unequal: 0.                                                                     
Number of Observations with All Compared Variables Equal: 4.                                                                        
                                                                                                                                    
NOTE: No unequal values were found. All values compared are exactly equal.                                                          
                                                                                &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 16:30:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873424#M345105</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-02T16:30:25Z</dc:date>
    </item>
    <item>
      <title>Re: Using formulas to sum values between rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873590#M345179</link>
      <description>Tom,&lt;BR /&gt;I think formula is changing dynamically , not just for all the obs. Maybe OP should explain more details about his question?</description>
      <pubDate>Wed, 03 May 2023 11:29:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-formulas-to-sum-values-between-rows/m-p/873590#M345179</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-05-03T11:29:09Z</dc:date>
    </item>
  </channel>
</rss>

