<?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: Sum variables with the same prefix in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529484#M144680</link>
    <description>I did, it worked fine. Post the actual code you used. Note that MONTH is not in the PROC MEANS, but is in the PROC SORT. SO the sum is happening by YEAR.</description>
    <pubDate>Wed, 23 Jan 2019 18:26:44 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-01-23T18:26:44Z</dc:date>
    <item>
      <title>Sum variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529316#M144595</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have monthly data and variable's names are formated with structure of year and month, for example _19991 _19992 ... _199912.&lt;/P&gt;&lt;P&gt;Now I want to sum up 12 months to get annual data. I wrote code below and it works but I wonder whether there is a way to reduce 5 lines to 1 line only.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA MB.MB_1995_1999_ANNUAL;
SET MB.MB_1995_1999_MONTHLY;
_1995 = SUM(OF _1995:);
_1996 = SUM(OF _1996:);
_1997 = SUM(OF _1997:);
_1998 = SUM(OF _1998:);
_1999 = SUM(OF _1999:);
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can anyone help me? Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Jan 2019 02:38:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529316#M144595</guid>
      <dc:creator>CindyVu</dc:creator>
      <dc:date>2019-01-23T02:38:29Z</dc:date>
    </item>
    <item>
      <title>Re: Sum variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529322#M144597</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt; I wonder whether there is a way to reduce 5 lines to 1 line only.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;No there isn't. You could build a loop, but why?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;What you can do to improve you code though is not write everything in uppercase.&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>Wed, 23 Jan 2019 03:23:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529322#M144597</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-01-23T03:23:11Z</dc:date>
    </item>
    <item>
      <title>Re: Sum variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529323#M144598</link>
      <description>Not really. You could do an array, but with just 5, you'll end up with the same thing anyways. &lt;BR /&gt;&lt;BR /&gt;If you want a more dynamic approach that will work, I suggest switching your data to a long format in general  and then this can easily be accomplished in one PROC.</description>
      <pubDate>Wed, 23 Jan 2019 03:23:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529323#M144598</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-01-23T03:23:37Z</dc:date>
    </item>
    <item>
      <title>Re: Sum variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529328#M144602</link>
      <description>&lt;P&gt;Hi, I am very new to SAS. Why shouldn't I write everything in uppercase?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For this situation, I have many files with the same format like this. So If I can reduce, it will save more time than modifying every 5 lines when using new data file.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Jan 2019 04:09:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529328#M144602</guid>
      <dc:creator>CindyVu</dc:creator>
      <dc:date>2019-01-23T04:09:01Z</dc:date>
    </item>
    <item>
      <title>Re: Sum variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529331#M144604</link>
      <description>&lt;P&gt;Hi ,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, must 5 is not a big problem. But I have many files with the same format like this, I think it can be some more general codes so I can save time modifying every 5 lines.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to use array but it didn't work. I am new with SAS so still not familiar with using array. Can you write codes for me? My data looks like this&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="31108350_239160709981430_1781197597067706368_n.jpg" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/26502iE67ACDE5BFB2D499/image-size/large?v=v2&amp;amp;px=999" role="button" title="31108350_239160709981430_1781197597067706368_n.jpg" alt="31108350_239160709981430_1781197597067706368_n.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Jan 2019 04:11:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529331#M144604</guid>
      <dc:creator>CindyVu</dc:creator>
      <dc:date>2019-01-23T04:11:14Z</dc:date>
    </item>
    <item>
      <title>Re: Sum variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529333#M144606</link>
      <description>If you need to deal with this regularly and across more dates I strongly suggest transposing your data, proc transpose, then use PROC MEANS to calculate things more easily. &lt;BR /&gt;&lt;BR /&gt;I can’t work with images. If you need help with code post actual data as text, preferably as a data step. There’s instructions on the forum on how to do so if you do a quick search.</description>
      <pubDate>Wed, 23 Jan 2019 04:18:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529333#M144606</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-01-23T04:18:18Z</dc:date>
    </item>
    <item>
      <title>Re: Sum variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529336#M144607</link>
      <description>&lt;P&gt;Oh, actually I plan to transpose after calculating because I think it's easier to sum up annual data first.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data file is attached below. Can you take a look and suggest some code lines? Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 23 Jan 2019 04:52:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529336#M144607</guid>
      <dc:creator>CindyVu</dc:creator>
      <dc:date>2019-01-23T04:52:11Z</dc:date>
    </item>
    <item>
      <title>Re: Sum variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529337#M144608</link>
      <description>It’s easier after. Try a transpose and proc means.</description>
      <pubDate>Wed, 23 Jan 2019 04:56:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529337#M144608</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-01-23T04:56:00Z</dc:date>
    </item>
    <item>
      <title>Re: Sum variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529426#M144653</link>
      <description>&lt;P&gt;Here's the transpose and sum method:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data long;
set temp;

array _d(*) _20:;

do i=1 to dim(_d);
Year = input(substr(vname(_d(i)), 2, 4), 8.);
Month = input(substr(vname(_d(i)), 6), 8.);
Value = _d(i);
output;
end;

drop _2:;
run;

proc sort data=long;
by 
 Company_Name   CUSIP   Primary_SIC_Code 
 Industry_Sector_Code   Intl_Securities_ID   SEDOL_Code  year month;
 run;

proc means data=long nway noprint;
by 
 Company_Name   CUSIP   Primary_SIC_Code 
 Industry_Sector_Code   Intl_Securities_ID   SEDOL_Code  year;
 var value;
 output out=want sum= ;
 run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240866"&gt;@CindyVu&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Oh, actually I plan to transpose after calculating because I think it's easier to sum up annual data first.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My data file is attached below. Can you take a look and suggest some code lines? Thank you&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Jan 2019 15:57:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529426#M144653</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-01-23T15:57:04Z</dc:date>
    </item>
    <item>
      <title>Re: Sum variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529483#M144679</link>
      <description>Hi, I tried your codes, but after running PROC MEANS, the result does not show sum of every 12 months. Can you check it again?</description>
      <pubDate>Wed, 23 Jan 2019 18:25:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529483#M144679</guid>
      <dc:creator>CindyVu</dc:creator>
      <dc:date>2019-01-23T18:25:37Z</dc:date>
    </item>
    <item>
      <title>Re: Sum variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529484#M144680</link>
      <description>I did, it worked fine. Post the actual code you used. Note that MONTH is not in the PROC MEANS, but is in the PROC SORT. SO the sum is happening by YEAR.</description>
      <pubDate>Wed, 23 Jan 2019 18:26:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529484#M144680</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-01-23T18:26:44Z</dc:date>
    </item>
    <item>
      <title>Re: Sum variables with the same prefix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529486#M144682</link>
      <description>Oh yes, my bad. I used month in both MEANS and SORT so it didn't work. Thank you so much for your help!</description>
      <pubDate>Wed, 23 Jan 2019 18:29:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-variables-with-the-same-prefix/m-p/529486#M144682</guid>
      <dc:creator>CindyVu</dc:creator>
      <dc:date>2019-01-23T18:29:26Z</dc:date>
    </item>
  </channel>
</rss>

