<?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 How to make a summary for many columns and name the summary variables  elegantly? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-summary-for-many-columns-and-name-the-summary/m-p/940949#M369280</link>
    <description>&lt;P&gt;dear all:&lt;/P&gt;
&lt;P&gt;I want to summary a sales data from different area ( for example sum_20240105 summary the s_20240105 and the prefix 'sum_' is a must.There are lots of columns)，how can I code it not that hard?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sales;
input area $ s_20240105 s_20240109 s_20240112 s_20240122 s_20240129 s_20240209 s_20240212 s_20240222;
datalines;
NO1 1 2 4 5 6 7 8 9
NO2 2 5 9 5 8 6 8 10
NO3 2 4 6 8 7 4 9 12
;
run;

data want;
set sales end=last;
sum_20240105+s_20240105;
sum_20240109+s_20240109;
sum_20240112+s_20240112;
sum_20240122+s_20240122;
sum_20240129+s_20240129;
sum_20240209+s_20240209;
sum_20240212+s_20240212;
sum_20240222+s_20240222;
if last then output;
keep  sum_:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The want data is:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="捕获.JPG" style="width: 736px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/99640iAE38EF812D10128F/image-size/large?v=v2&amp;amp;px=999" role="button" title="捕获.JPG" alt="捕获.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 27 Aug 2024 02:30:18 GMT</pubDate>
    <dc:creator>duanzongran</dc:creator>
    <dc:date>2024-08-27T02:30:18Z</dc:date>
    <item>
      <title>How to make a summary for many columns and name the summary variables  elegantly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-summary-for-many-columns-and-name-the-summary/m-p/940949#M369280</link>
      <description>&lt;P&gt;dear all:&lt;/P&gt;
&lt;P&gt;I want to summary a sales data from different area ( for example sum_20240105 summary the s_20240105 and the prefix 'sum_' is a must.There are lots of columns)，how can I code it not that hard?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sales;
input area $ s_20240105 s_20240109 s_20240112 s_20240122 s_20240129 s_20240209 s_20240212 s_20240222;
datalines;
NO1 1 2 4 5 6 7 8 9
NO2 2 5 9 5 8 6 8 10
NO3 2 4 6 8 7 4 9 12
;
run;

data want;
set sales end=last;
sum_20240105+s_20240105;
sum_20240109+s_20240109;
sum_20240112+s_20240112;
sum_20240122+s_20240122;
sum_20240129+s_20240129;
sum_20240209+s_20240209;
sum_20240212+s_20240212;
sum_20240222+s_20240222;
if last then output;
keep  sum_:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The want data is:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="捕获.JPG" style="width: 736px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/99640iAE38EF812D10128F/image-size/large?v=v2&amp;amp;px=999" role="button" title="捕获.JPG" alt="捕获.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2024 02:30:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-summary-for-many-columns-and-name-the-summary/m-p/940949#M369280</guid>
      <dc:creator>duanzongran</dc:creator>
      <dc:date>2024-08-27T02:30:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to make a summary for many columns and name the summary variables  elegantly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-summary-for-many-columns-and-name-the-summary/m-p/940950#M369281</link>
      <description>&lt;P&gt;Just ask PROC SUMMARY to do that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=sales nway ;
   var s_: ;
   output out=want sum=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Will produce a dataset like you asked for (only using the original variable names).&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does it really matter if the names start with SUM instead of S? Why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It will probably be MUCH easier if you move that numeric suffix (that looks like a DATE string) out of the variable NAME and into its own variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=sales out=sales_t(rename=(col1=sales)) name=date_char ;
  by area ;
  var s_: ;
run;

data sales_t;
  set sales_t;
  date = input(substr(date_char,3),yymmdd8.);
  format date yymmdd10.;
run;

proc summary data=sales_t nway;
  class date;
  var sales ;
  output out=want_t sum=sum_sales;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;                                          sum_
Obs          date    _TYPE_    _FREQ_    sales

 1     2024-01-05       1         3         5
 2     2024-01-09       1         3        11
 3     2024-01-12       1         3        19
 4     2024-01-22       1         3        18
 5     2024-01-29       1         3        21
 6     2024-02-09       1         3        17
 7     2024-02-12       1         3        25
 8     2024-02-22       1         3        31

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2024 03:13:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-summary-for-many-columns-and-name-the-summary/m-p/940950#M369281</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-27T03:13:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to make a summary for many columns and name the summary variables  elegantly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-summary-for-many-columns-and-name-the-summary/m-p/940952#M369283</link>
      <description>&lt;P&gt;Use proc summary, with a statistic-rename capability.&amp;nbsp; For instance, if you have only two variables you could:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=sales;
  var s_20240105 s_20240109;
  output out=want   sum(s_20240105 s_20240109) = SUM_20240105 SUM_20240109;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But you have a lot of variables to be renamed.&amp;nbsp; Use the dictionary.columns capability of PROC SQL to build macrovars &amp;amp;VARLIST and &amp;amp;SUMLIST&amp;nbsp; to generate the rename components:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sales;
input area $ s_20240105 s_20240109 s_20240112 s_20240122 s_20240129 s_20240209 s_20240212 s_20240222;
datalines;
NO1 1 2 4 5 6 7 8 9
NO2 2 5 9 5 8 6 8 10
NO3 2 4 6 8 7 4 9 12
run;

proc sql noprint;
  select distinct 
         name                       ,cats('SUM_',scan(name,2,'_'))
  into   :varlist separated by ' '  ,:sumlist separated by ' '
  from dictionary.columns
  where libname='WORK' and memname='SALES' and upcase(scan(name,1,'_'))='S';
quit;
%put &amp;amp;=varlist;
%put &amp;amp;=sumlist;

proc summary data=sales;
  var s_:;
  output out=want (drop=_type_ _freq_) sum(&amp;amp;varlist)=&amp;amp;sumlist;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Aug 2024 03:45:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-summary-for-many-columns-and-name-the-summary/m-p/940952#M369283</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-08-27T03:45:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to make a summary for many columns and name the summary variables  elegantly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-summary-for-many-columns-and-name-the-summary/m-p/940963#M369288</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2024 06:06:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-make-a-summary-for-many-columns-and-name-the-summary/m-p/940963#M369288</guid>
      <dc:creator>duanzongran</dc:creator>
      <dc:date>2024-08-27T06:06:21Z</dc:date>
    </item>
  </channel>
</rss>

