<?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: How do I calculated and add back totals by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculated-and-add-back-totals-by-group/m-p/568042#M159826</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/278781"&gt;@kavange&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;1. Proc SQL will compute the aggregates for you and remerge them back with the original data in the same step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                             
   input child year Age count wage ;                                                    
   cards ;                                                                              
0 2017 25 100 12.5                                                                      
0 2017 30  20 20.4                                                                      
0 2017 38  60 15                                                                        
0 2018 22  40 12.5                                                                      
0 2018 28  80 10.5                                                                      
&lt;FONT color="#0000FF"&gt;0 2017 37  10 15&lt;/FONT&gt;                                                                        
1 2017 40  40 12.5                                                                      
1 2017 28  80 10.5                                                                      
1 2017 35  10 15                                                                        
1 2018 44  40 12.5                                                                      
1 2018 22  50 10.5                                                                      
1 2018 37  10 15                                                                        
run ;                                                                                   
                                                                                        
proc sql ;                                                                              
  create table want as select *, sum (count) as sum_count from have group  child, year ;
quit ;                                                                                  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2. If your input data set &lt;EM&gt;were&lt;/EM&gt; sorted by child and year (which it's not because of record #6 - maybe you didn't intend that), it would be a typical DATA step job for the double DoW-loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                
   input child year Age count wage ;       
   cards ;                                 
0 2017 25 100 12.5                         
0 2017 30  20 20.4                         
0 2017 38  60 15                           
&lt;FONT color="#0000FF"&gt;0 2017 37  10 15&lt;/FONT&gt;                           
0 2018 22  40 12.5                         
0 2018 28  80 10.5                         
1 2017 40  40 12.5                         
1 2017 28  80 10.5                         
1 2017 35  10 15                           
1 2018 44  40 12.5                         
1 2018 22  50 10.5                         
1 2018 37  10 15                           
run ;                                      
                                           
data want ;                                
  do _n_ = 1 by 1 until (last.year) ;      
    set have ;                             
    by child year ;                        
    sum_count = sum (sum_count, count, 0) ;
  end ;                                    
  do _n_ = 1 to _n_ ;                      
    set have ;                             
    output ;                               
  end ;                                    
run ;                                      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;3. If your input file is completely unsorted, and especially if you want to do this job while preserving the original record order, then the hash object is just the ticket:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                    
   input child year Age count wage ;           
   cards ;                                     
1 2018 22  50 10.5                             
0 2017 30  20 20.4                             
0 2017 38  60 15                               
1 2017 40  40 12.5                             
0 2018 28  80 10.5                             
1 2017 28  80 10.5                             
0 2017 25 100 12.5                             
1 2017 35  10 15                               
0 2018 22  40 12.5                             
1 2018 44  40 12.5                             
0 2017 37  10 15                               
1 2018 37  10 15                               
run ;                                          
                                               
data want ;                                    
  if _n_ = 1 then do ;                         
    dcl hash h () ;                            
    h.definekey ("child", "year") ;            
    h.definedata ("sum_count") ;               
    h.definedone () ;                          
    do until (z) ;                             
      set have end = z ;                       
      if h.find() ne 0 then sum_count = count ;
      else                  sum_count + count ;
      h.replace() ;                            
    end ;                                      
  end ;                                        
  set have ;                                   
  h.find() ;                                   
run ;                                          
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 21 Jun 2019 19:05:53 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-06-21T19:05:53Z</dc:date>
    <item>
      <title>How do I calculated and add back totals by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculated-and-add-back-totals-by-group/m-p/567943#M159777</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using this example, could someone help me figure out how to calculate and add back the sum of 'count' by child and year. In other words, how do we add a column to the&amp;nbsp;table where the sum of 'count' is&amp;nbsp;calculated and repeated&amp;nbsp;for every row with the same combination of 'child' and 'year' values. I'm a new SAS user so please keep it simple!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; child&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;SPAN class="token statement"&gt;infile&lt;/SPAN&gt; datalines dsd truncover&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt; child:&lt;SPAN class="token punctuation"&gt;32&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; year:&lt;SPAN class="token number"&gt;32&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; Age:&lt;SPAN class="token number"&gt;32&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; count:&lt;SPAN class="token number"&gt;32&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;wage&lt;/SPAN&gt;:&lt;SPAN class="token number"&gt;32&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token keyword"&gt;datalines&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;0&amp;nbsp;2017&amp;nbsp;25&amp;nbsp;100 12.5 &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;0&amp;nbsp;2017&amp;nbsp;30 20 20.4 &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;0&amp;nbsp;2017&amp;nbsp;38&amp;nbsp;60&amp;nbsp;15 &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;0&amp;nbsp;2018&amp;nbsp;22&amp;nbsp;40 12.5 &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;0&amp;nbsp;2018&amp;nbsp;28&amp;nbsp;80 10.5&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;0&amp;nbsp;2017&amp;nbsp;37&amp;nbsp;10 15&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1&amp;nbsp;2017&amp;nbsp;40&amp;nbsp;40 12.5 &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1&amp;nbsp;2017&amp;nbsp;28&amp;nbsp;80 10.5&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1&amp;nbsp;2017&amp;nbsp;35&amp;nbsp;10 15 &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1&amp;nbsp;2018&amp;nbsp;44&amp;nbsp;40 12.5 &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1&amp;nbsp;2018 22&amp;nbsp;50 10.5&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1&amp;nbsp;2018&amp;nbsp;37&amp;nbsp;10 15 &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jun 2019 15:04:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculated-and-add-back-totals-by-group/m-p/567943#M159777</guid>
      <dc:creator>kavange</dc:creator>
      <dc:date>2019-06-21T15:04:27Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculated and add back totals by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculated-and-add-back-totals-by-group/m-p/567946#M159779</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=child nway;
    class child;
    var count;
    output out=_stats_ sum=sum_count;
run;
data want;
    merge child _stats_;
    by child;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Jun 2019 15:10:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculated-and-add-back-totals-by-group/m-p/567946#M159779</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-06-21T15:10:33Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculated and add back totals by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculated-and-add-back-totals-by-group/m-p/567952#M159783</link>
      <description>&lt;P&gt;One way&lt;/P&gt;
&lt;PRE&gt;data child;
   input child year Age count wage;
datalines; 
0 2017 25 100 12.5 
0 2017 30 20 20.4 
0 2017 38 60 15 
0 2018 22 40 12.5 
0 2018 28 80 10.5
0 2017 37 10 15
1 2017 40 40 12.5 
1 2017 28 80 10.5
1 2017 35 10 15 
1 2018 44 40 12.5 
1 2018 22 50 10.5
1 2018 37 10 15 
;
proc summary data=child nway;
   class child year;
   var count;
   output out=work.sum (drop=_:) sum=Total;
run;

proc sql;
   create table want as
   select child.child, child.year, child.age, child.count, child.wage
          ,t.total
   from child 
        left join
        work.sum as t
        on child.child=t.child
        and child.year=t.year
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;Please post code in a code box. The forum will reformat pasted text. That was the possible reason why your "as posted" code would not run correctly for the data step. So mine looks a little different than yours.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I use the Proc summary to create a set with summed counts and Proc SQL to join things as you have multiple records with the same child and year and SQL does a better job aligning things.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The summary could be done in a subquery within the proc sql but I think this approach is "simpler".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The alias bit in the proc sql, work.sum as t allows using the letter t as short hand for referencing the data set work.sum. You can't use a two-level data set name with the .&amp;lt;variable name&amp;gt; due to syntax rules. So the alias is needed for work.sum. A single level name for a data set can be used as with the CHILD data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jun 2019 15:17:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculated-and-add-back-totals-by-group/m-p/567952#M159783</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-06-21T15:17:34Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculated and add back totals by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculated-and-add-back-totals-by-group/m-p/567962#M159790</link>
      <description>There are several methods, PROC MEANS + MERGE and SQL being the most common. Both are illustrated by other users but I also have two examples here: &lt;BR /&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas&lt;/A&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 21 Jun 2019 15:32:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculated-and-add-back-totals-by-group/m-p/567962#M159790</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-21T15:32:17Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculated and add back totals by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculated-and-add-back-totals-by-group/m-p/568042#M159826</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/278781"&gt;@kavange&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;1. Proc SQL will compute the aggregates for you and remerge them back with the original data in the same step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                             
   input child year Age count wage ;                                                    
   cards ;                                                                              
0 2017 25 100 12.5                                                                      
0 2017 30  20 20.4                                                                      
0 2017 38  60 15                                                                        
0 2018 22  40 12.5                                                                      
0 2018 28  80 10.5                                                                      
&lt;FONT color="#0000FF"&gt;0 2017 37  10 15&lt;/FONT&gt;                                                                        
1 2017 40  40 12.5                                                                      
1 2017 28  80 10.5                                                                      
1 2017 35  10 15                                                                        
1 2018 44  40 12.5                                                                      
1 2018 22  50 10.5                                                                      
1 2018 37  10 15                                                                        
run ;                                                                                   
                                                                                        
proc sql ;                                                                              
  create table want as select *, sum (count) as sum_count from have group  child, year ;
quit ;                                                                                  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2. If your input data set &lt;EM&gt;were&lt;/EM&gt; sorted by child and year (which it's not because of record #6 - maybe you didn't intend that), it would be a typical DATA step job for the double DoW-loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                
   input child year Age count wage ;       
   cards ;                                 
0 2017 25 100 12.5                         
0 2017 30  20 20.4                         
0 2017 38  60 15                           
&lt;FONT color="#0000FF"&gt;0 2017 37  10 15&lt;/FONT&gt;                           
0 2018 22  40 12.5                         
0 2018 28  80 10.5                         
1 2017 40  40 12.5                         
1 2017 28  80 10.5                         
1 2017 35  10 15                           
1 2018 44  40 12.5                         
1 2018 22  50 10.5                         
1 2018 37  10 15                           
run ;                                      
                                           
data want ;                                
  do _n_ = 1 by 1 until (last.year) ;      
    set have ;                             
    by child year ;                        
    sum_count = sum (sum_count, count, 0) ;
  end ;                                    
  do _n_ = 1 to _n_ ;                      
    set have ;                             
    output ;                               
  end ;                                    
run ;                                      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;3. If your input file is completely unsorted, and especially if you want to do this job while preserving the original record order, then the hash object is just the ticket:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                    
   input child year Age count wage ;           
   cards ;                                     
1 2018 22  50 10.5                             
0 2017 30  20 20.4                             
0 2017 38  60 15                               
1 2017 40  40 12.5                             
0 2018 28  80 10.5                             
1 2017 28  80 10.5                             
0 2017 25 100 12.5                             
1 2017 35  10 15                               
0 2018 22  40 12.5                             
1 2018 44  40 12.5                             
0 2017 37  10 15                               
1 2018 37  10 15                               
run ;                                          
                                               
data want ;                                    
  if _n_ = 1 then do ;                         
    dcl hash h () ;                            
    h.definekey ("child", "year") ;            
    h.definedata ("sum_count") ;               
    h.definedone () ;                          
    do until (z) ;                             
      set have end = z ;                       
      if h.find() ne 0 then sum_count = count ;
      else                  sum_count + count ;
      h.replace() ;                            
    end ;                                      
  end ;                                        
  set have ;                                   
  h.find() ;                                   
run ;                                          
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jun 2019 19:05:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculated-and-add-back-totals-by-group/m-p/568042#M159826</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-06-21T19:05:53Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculated and add back totals by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculated-and-add-back-totals-by-group/m-p/568044#M159827</link>
      <description>&lt;P&gt;Thank you so much!! This solved my issue.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jun 2019 19:10:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculated-and-add-back-totals-by-group/m-p/568044#M159827</guid>
      <dc:creator>kavange</dc:creator>
      <dc:date>2019-06-21T19:10:24Z</dc:date>
    </item>
  </channel>
</rss>

