<?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: summarized data: summing by group for some columns and report last entry for other columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574095#M162144</link>
    <description>&lt;P&gt;Thanks for the quick replies.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please note, in this example the computation is done on 6 columns and the suggested solutions so far work fine in this case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What if I have to do the same on 200+ columns of which some need to be summed and for others the last entry will be reported?&lt;/P&gt;&lt;P&gt;Do I need to define the columns which used last.id in a separate table or perhaps using hash object?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 17 Jul 2019 08:55:59 GMT</pubDate>
    <dc:creator>krish_101</dc:creator>
    <dc:date>2019-07-17T08:55:59Z</dc:date>
    <item>
      <title>summarized data: summing by group for some columns and report last entry for other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574088#M162141</link>
      <description>&lt;P&gt;I want to group by one column, by "id" in this example, to compute sum for some columns and report the last entry for some other columns (those columns would have to be specified).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Say I have this data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id time sales bonus commission toalstock totalcash totalvalue;
     datalines;
       1   1   10     1        1         100       50        200
       1   2   10     1        1         100       50        200
       1   3   15     2        1          80       50        200
       1   4   10     1        1          90       50        200
       1   5   20     2        2         100       50        200
       1   6   23     2        2         133       52        222
       ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My desired output would be:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input id time sales bonus commission toalstock totalcash totalvalue;
       1    6    88    9        8        133        52        222
       ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My main concern is computational efficiency as I'll be running this on tables with many more columns (about 200) and data entry (about 30000 rows) with different ids.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc SQL or native SAS are both welcome.&lt;/P&gt;&lt;P&gt;Rows might be shuffled if necessary.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Krish&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2019 08:29:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574088#M162141</guid>
      <dc:creator>krish_101</dc:creator>
      <dc:date>2019-07-17T08:29:10Z</dc:date>
    </item>
    <item>
      <title>summing multiple variable by group and report last entry for other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574085#M162173</link>
      <description>&lt;P&gt;I want to group by one column, by "id" in this example, to compute sum for some columns and report the last entry for some other columns (those columns would have to be specified).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Say I have this data:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id time sales bonus commission toalstock totalcash totalvalue;&lt;BR /&gt; datalines;
       1   1   10     1        1         100       50        200
       1   2   10     1        1         100       50        200
       1   3   15     2        1          80       50        200
       1   4   10     1        1          90       50        200
       1   5   20     2        2         100       50        200
       1   6   23     2        2         133       52        222&lt;BR /&gt;       &lt;BR /&gt;       2...                                                            &lt;BR /&gt;       ...
       3.. 
       ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;My desired output would be:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input id time sales bonus commission toalstock totalcash totalvalue;
       1    6    88    9        8        133        52        222
       2...
       3...&lt;BR /&gt;       ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;My main concern is computational efficiency as I'll be running this on tables with&lt;/SPAN&gt; many more columns (about 200) and data entry (about 30000 rows).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc SQL or native SAS are both welcome.&lt;/P&gt;&lt;P&gt;Rows might be shuffled if necessary.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Krish&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2019 08:14:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574085#M162173</guid>
      <dc:creator>krish_101</dc:creator>
      <dc:date>2019-07-17T08:14:02Z</dc:date>
    </item>
    <item>
      <title>Re: summarized data: summing by group for some columns and report last entry for other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574090#M162142</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_:);
    do until (last.id);
        set have;
        by id;
        _sales+sales; _bonus+bonus; _commission+commission;
    end;
    sales=_sales; bonus=_bonus; commission=_commission;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Jul 2019 08:34:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574090#M162142</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-07-17T08:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: summarized data: summing by group for some columns and report last entry for other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574091#M162143</link>
      <description>&lt;P&gt;My preferred way of doing this is a data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id time sales bonus commission toalstock totalcash totalvalue;
     datalines;
       1   1   10     1        1         100       50        200
       1   2   10     1        1         100       50        200
       1   3   15     2        1          80       50        200
       1   4   10     1        1          90       50        200
       1   5   20     2        2         100       50        200
       1   6   23     2        2         133       52        222
       ;

data want;
retain id time sales bonus commission toalstock totalcash totalvalue;
set have (
  rename=(sales=_sales bonus=_bonus commission=_commission)
);
by id;
if first.id
then do;
  sales = _sales;
  bonus = _bonus;
  commission = _commission;
end;
else do;
  sales + _sales;
  bonus + _bonus;
  commission + _commission;
end;
if last.id;
drop _:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Jul 2019 08:44:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574091#M162143</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-17T08:44:18Z</dc:date>
    </item>
    <item>
      <title>Re: summarized data: summing by group for some columns and report last entry for other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574095#M162144</link>
      <description>&lt;P&gt;Thanks for the quick replies.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please note, in this example the computation is done on 6 columns and the suggested solutions so far work fine in this case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What if I have to do the same on 200+ columns of which some need to be summed and for others the last entry will be reported?&lt;/P&gt;&lt;P&gt;Do I need to define the columns which used last.id in a separate table or perhaps using hash object?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2019 08:55:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574095#M162144</guid>
      <dc:creator>krish_101</dc:creator>
      <dc:date>2019-07-17T08:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: summarized data: summing by group for some columns and report last entry for other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574099#M162147</link>
      <description>What if I have to do the same on 200+ columns of which some need to be summed and for others the last entry will be reported?&lt;BR /&gt;&lt;BR /&gt;Do I need to define the columns which used last.id in a separate table or using hash object?</description>
      <pubDate>Wed, 17 Jul 2019 09:06:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574099#M162147</guid>
      <dc:creator>krish_101</dc:creator>
      <dc:date>2019-07-17T09:06:49Z</dc:date>
    </item>
    <item>
      <title>Re: summarized data: summing by group for some columns and report last entry for other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574100#M162148</link>
      <description>&lt;P&gt;How many of those +200 columns do you want to sum?&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2019 09:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574100#M162148</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-07-17T09:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: summarized data: summing by group for some columns and report last entry for other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574102#M162149</link>
      <description>about 100 columns to be summed&lt;BR /&gt;and 100 other columns where last id is reported.</description>
      <pubDate>Wed, 17 Jul 2019 09:20:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574102#M162149</guid>
      <dc:creator>krish_101</dc:creator>
      <dc:date>2019-07-17T09:20:48Z</dc:date>
    </item>
    <item>
      <title>Re: summarized data: summing by group for some columns and report last entry for other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574104#M162150</link>
      <description>&lt;P&gt;Ok. Well, the logic posted by both me and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;holds even for several hundred variables. Obviously, you would have to type out the variables that you want to sum. So unless there is some naming convention that is similar among the sum variables, I don't think you will gain much from using an array or a hash object.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2019 09:25:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574104#M162150</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-07-17T09:25:45Z</dc:date>
    </item>
    <item>
      <title>Re: summarized data: summing by group for some columns and report last entry for other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574105#M162151</link>
      <description>&lt;P&gt;As you can see, my code does not use a hash object for 3 variables, so you won't need it for 200 either.&lt;/P&gt;
&lt;P&gt;You have to play the rename-initialize-sum-drop game for all the variables that need summing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note:&lt;/P&gt;
&lt;P&gt;The retain statement in my code is not necessary; it is mostly there to keep the horizontal order of columns. The retain for tzhe newly created variables is implicitly declared by using the increment statements.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2019 09:26:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574105#M162151</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-17T09:26:16Z</dc:date>
    </item>
    <item>
      <title>Re: summarized data: summing by group for some columns and report last entry for other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574109#M162155</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/173065"&gt;@krish_101&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;about 100 columns to be summed&lt;BR /&gt;and 100 other columns where last id is reported.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;EM&gt;You&lt;/EM&gt; are the coder, so it's up to you to tell SAS what to do. If you have some resource that can make the code data-driven (list(s) of variable names in a dataset), then we can show you how to utilize that to automatically create the code.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2019 09:42:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574109#M162155</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-17T09:42:36Z</dc:date>
    </item>
    <item>
      <title>Re: summarized data: summing by group for some columns and report last entry for other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574111#M162157</link>
      <description>&lt;P&gt;I have found a way to utilize proc summary in taking the last value of a group:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id time sales bonus commission toalstock totalcash totalvalue;
     datalines;
       1   1   10     1        1         100       50        200
       1   2   10     1        1         100       50        200
       1   3   15     2        1          80       50        200
       1   4   10     1        1          90       50        200
       1   5   20     2        2         100       50        200
       1   6   23     2        2         133       52        222
       ;

proc summary data=have;
by id;
output
  out=want (drop=_type_ _freq_)
  sum(sales bonus commission)=
  idgroup (last out (time toalstock totalcash totalvalue)=)
;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;id    sales    bonus    commission    time    toalstock    totalcash    totalvalue

 1      88       9           8          6        133           52           222   
&lt;/PRE&gt;
&lt;P&gt;As you can see, you now "only" need to populate the sum()= and idgroup() options with the respective variable names.&lt;/P&gt;
&lt;P&gt;If you want to keep the original horizontal order of variables, you will need another data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set
  have (obs=0) /* retrieves the order of variables */
  want /* retrieves the data */
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Jul 2019 09:48:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574111#M162157</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-17T09:48:48Z</dc:date>
    </item>
    <item>
      <title>Re: summarized data: summing by group for some columns and report last entry for other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574113#M162159</link>
      <description>&lt;P&gt;The following step is a small modification of what &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; posted: it uses two arrays for the variables the need to be summed. Wrapping the step in a macro to further reduce listing the variables should be the next step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   retain id time sales bonus commission toalstock totalcash totalvalue;
   set have (
      rename=(sales=_sales bonus=_bonus commission=_commission)
      );
   by id;

   array vars _sales _bonus _commission;
   array sums sales bonus commission;

   if first.id then do;
      do _i = 1 to dim(vars);
         sums[_i] = vars[_i];
      end;
   end;
   else do;
      do _i = 1 to dim(vars);
         sums[_i] + vars[_i];
      end;
   end;

   if last.id;
   drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Jul 2019 09:52:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574113#M162159</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-07-17T09:52:05Z</dc:date>
    </item>
    <item>
      <title>Re: summarized data: summing by group for some columns and report last entry for other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574119#M162162</link>
      <description>&lt;P&gt;looks great&lt;BR /&gt;I created a list(s) of variable names in a dataset called valist which groups the columns to be summed under sumvars&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data varlist;
input  sumvars         lastvars;
          name1         name2
          name5         name3&lt;BR /&gt;&lt;BR /&gt;          ...           ...
          name70        name50
          name100       name102
;
&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;select sumvars into : sumvarlist separated by " "&lt;BR /&gt;from varlist&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;quit;&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;select lastvars into : lastvarlist separated by " "&lt;BR /&gt;from varlist&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;modify this part of the code&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token punctuation"&gt;&lt;BR /&gt; &lt;SPAN class="token function"&gt;sum&lt;/SPAN&gt;(&amp;amp;&lt;CODE class=" language-sas"&gt;sumvarlist&lt;/CODE&gt;)&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; idgroup (last out (&amp;amp;last&lt;CODE class=" language-sas"&gt;varlist&lt;/CODE&gt;)&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;)&lt;BR /&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2019 10:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summarized-data-summing-by-group-for-some-columns-and-report/m-p/574119#M162162</guid>
      <dc:creator>krish_101</dc:creator>
      <dc:date>2019-07-17T10:38:39Z</dc:date>
    </item>
  </channel>
</rss>

