<?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: Looping through columns to get sum of each column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626353#M184731</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;Why not use _NUMERIC_?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
var _numeric_;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 20 Feb 2020 21:20:55 GMT</pubDate>
    <dc:creator>data_null__</dc:creator>
    <dc:date>2020-02-20T21:20:55Z</dc:date>
    <item>
      <title>Looping through columns to get sum of each column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626342#M184723</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset with more than 20 columns and I want to get the sum of each column without writing sum for over 20 times.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I made a sample dataset as follows&lt;/P&gt;&lt;P&gt;(the actual dataset will have much more columns, and there's not a particular order of column names):&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;P&gt;data sample;&lt;BR /&gt;infile datalines delimiter=',';&lt;BR /&gt;input Email $ code1 code3 codeA;&lt;BR /&gt;datalines;&lt;BR /&gt;E1,1,0,1&lt;BR /&gt;E2,1,1,0&lt;BR /&gt;E3,0,1,1&lt;BR /&gt;E4,0,0,1&lt;BR /&gt;E5,0,0,0&lt;BR /&gt;E6,1,1,1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;P&gt;what I want to get,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;code1&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;code3&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;codeA&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;3&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;3&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;4&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;********************************************&lt;/P&gt;&lt;P&gt;I know I can do:&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;select sum(code1) as Code1, sum(code3) as Code3, sum(codeA) as CodeA&lt;/P&gt;&lt;P&gt;from sample;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But when there are so many columns, can I do some looping in sas in order to make it more efficient?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also tried:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data OUT;&lt;BR /&gt;set sample;&lt;BR /&gt;array code code1 -- codeA;&lt;BR /&gt;do over code;&lt;BR /&gt;code = sum(code);&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But it still displays every email, and give me the same dataset as sample. Don' know how I can modify the code to make it work.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Feb 2020 21:00:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626342#M184723</guid>
      <dc:creator>qqian</dc:creator>
      <dc:date>2020-02-20T21:00:05Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through columns to get sum of each column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626343#M184724</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data sample;
infile datalines delimiter=',';
input Email $ code1 code3 codeA;
datalines;
E1,1,0,1
E2,1,1,0
E3,0,1,1
E4,0,0,1
E5,0,0,0
E6,1,1,1
;
run;

proc summary data=sample ;
var code:;
output out=want(keep=code:) sum=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SAS recommendation is that, when the requirement is to summarize, the 1st place to look is procedures summary/freq etc&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Feb 2020 21:07:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626343#M184724</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-20T21:07:36Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through columns to get sum of each column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626345#M184725</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; ,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the reply!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My follow up question is, what if there's no common part of the columns? They don't start with 'code'. For example, column names are: AA, BB,SJ, EWK.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Feb 2020 21:11:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626345#M184725</guid>
      <dc:creator>qqian</dc:creator>
      <dc:date>2020-02-20T21:11:12Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through columns to get sum of each column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626349#M184727</link>
      <description>&lt;P&gt;Hmm, that's a pain. If you do know the start to end sequence present in the dataset, you could list like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;var aa--ewk;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the above assumes the list is in continuous sequence from left to right in terms of position of the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there is no such a thing and all of these are completely random names and in any random order/position, i'm afraid there isn't much one can do about it. &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Feb 2020 21:16:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626349#M184727</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-20T21:16:48Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through columns to get sum of each column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626350#M184728</link>
      <description>&lt;P&gt;Then you can modify the code as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=sample ;
var gorilla giraffe octopus cobra elephant oryx lion jackal;
output out=want sum=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;where you type the variable names in the VAR statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If they are consecutive columns in the data set named SAMPLE, then it gets even easier&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=sample ;
var gorilla--jackal;
output out=want sum=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Feb 2020 21:17:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626350#M184728</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-20T21:17:09Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through columns to get sum of each column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626351#M184729</link>
      <description>I c. thanks!</description>
      <pubDate>Thu, 20 Feb 2020 21:18:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626351#M184729</guid>
      <dc:creator>qqian</dc:creator>
      <dc:date>2020-02-20T21:18:50Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through columns to get sum of each column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626352#M184730</link>
      <description>thank you Paige! That's also helpful!</description>
      <pubDate>Thu, 20 Feb 2020 21:19:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626352#M184730</guid>
      <dc:creator>qqian</dc:creator>
      <dc:date>2020-02-20T21:19:22Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through columns to get sum of each column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626353#M184731</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;Why not use _NUMERIC_?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
var _numeric_;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Feb 2020 21:20:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626353#M184731</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2020-02-20T21:20:55Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through columns to get sum of each column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626355#M184732</link>
      <description>&lt;P&gt;You could use _NUMERIC_, assuming you want all numeric variables to be summed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it is a huge data set, and you don't really need ALL of the numeric variables to be summed, for example there's no point in summing zip codes or dates,&amp;nbsp;&lt;EM&gt;etc&lt;/EM&gt;. then you are trading off execution time against programming/typing time.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Feb 2020 21:25:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626355#M184732</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-20T21:25:58Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through columns to get sum of each column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626359#M184733</link>
      <description>&lt;P&gt;Guru&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__&lt;/a&gt;&amp;nbsp; Honestly did think of it. To be honest, I once made a terrible mistake at work by using _numeric_&amp;nbsp; without knowing there were some variables(numeric) of no interest, though rectified it later but that's the problem. Of course I agree, should you have all vars of interest and only those vars in your dataset, _numeric_ is very comfy.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Feb 2020 21:31:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626359#M184733</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-20T21:31:00Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through columns to get sum of each column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626363#M184734</link>
      <description>&lt;P&gt;Here is a reference that illustrates how to refer to variables and datasets in a short cut list:&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Feb 2020 21:53:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-columns-to-get-sum-of-each-column/m-p/626363#M184734</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-02-20T21:53:38Z</dc:date>
    </item>
  </channel>
</rss>

