<?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 SAS Macro sum &amp;amp; create new variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-sum-amp-create-new-variables/m-p/320192#M270980</link>
    <description>&lt;P&gt;I need to create sum of 4 variables multiple times each time with new set of variables. For e.g. A1=sum(a1,a2,a3,a4),B1=sum(b1,b2,b3,b4) &amp;amp; so on. So , I am trying to write a macro that will help me do it easily. Following is the code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Say I have the variables as follows:&lt;BR /&gt;A1 A2 A3 A4 B1 B2 B3 B4&lt;BR /&gt;1  2  2  4  5  7  8  9&lt;BR /&gt;4  6  7  8  6  9  10 11&lt;BR /&gt;&lt;BR /&gt;I need 2 additional columns &amp;amp; my new dataset should look like:&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;A1 A2 A3 A4 B1 B2 B3 B4 A B&lt;BR /&gt;1  2  2  4  5  7  8  9  9 29&lt;BR /&gt;4  6  7  8  6  9  10 11 25 36&lt;BR /&gt;Can this be done?&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;BR /&gt;&lt;BR /&gt;%macro SUM2(VAR1,var2,var3,VAR4); data Subs_60_new; set Subs_60; substr(&amp;amp;var1,1,10)=sum(&amp;amp;var1,&amp;amp;var2,&amp;amp;var3,&amp;amp;var4); run; %mend sum2; options mprint mlogic;&lt;BR /&gt;%sum2(ADDITIONAL_INFO_Q1,ADDITIONAL_INFO_Q2,ADDITIONAL_INFO_Q3,ADDITIONAL_INFO_Q4); &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I&lt;/P&gt;</description>
    <pubDate>Tue, 20 Dec 2016 12:03:43 GMT</pubDate>
    <dc:creator>Lopa2016</dc:creator>
    <dc:date>2016-12-20T12:03:43Z</dc:date>
    <item>
      <title>SAS Macro sum &amp; create new variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-sum-amp-create-new-variables/m-p/320192#M270980</link>
      <description>&lt;P&gt;I need to create sum of 4 variables multiple times each time with new set of variables. For e.g. A1=sum(a1,a2,a3,a4),B1=sum(b1,b2,b3,b4) &amp;amp; so on. So , I am trying to write a macro that will help me do it easily. Following is the code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Say I have the variables as follows:&lt;BR /&gt;A1 A2 A3 A4 B1 B2 B3 B4&lt;BR /&gt;1  2  2  4  5  7  8  9&lt;BR /&gt;4  6  7  8  6  9  10 11&lt;BR /&gt;&lt;BR /&gt;I need 2 additional columns &amp;amp; my new dataset should look like:&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;A1 A2 A3 A4 B1 B2 B3 B4 A B&lt;BR /&gt;1  2  2  4  5  7  8  9  9 29&lt;BR /&gt;4  6  7  8  6  9  10 11 25 36&lt;BR /&gt;Can this be done?&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;BR /&gt;&lt;BR /&gt;%macro SUM2(VAR1,var2,var3,VAR4); data Subs_60_new; set Subs_60; substr(&amp;amp;var1,1,10)=sum(&amp;amp;var1,&amp;amp;var2,&amp;amp;var3,&amp;amp;var4); run; %mend sum2; options mprint mlogic;&lt;BR /&gt;%sum2(ADDITIONAL_INFO_Q1,ADDITIONAL_INFO_Q2,ADDITIONAL_INFO_Q3,ADDITIONAL_INFO_Q4); &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I&lt;/P&gt;</description>
      <pubDate>Tue, 20 Dec 2016 12:03:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-sum-amp-create-new-variables/m-p/320192#M270980</guid>
      <dc:creator>Lopa2016</dc:creator>
      <dc:date>2016-12-20T12:03:43Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro sum &amp; create new variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-sum-amp-create-new-variables/m-p/320198#M270981</link>
      <description>&lt;P&gt;Your probably over thinking the problem, just use arrays or lists of variables:&lt;/P&gt;
&lt;PRE&gt;data have;
  input A1 A2 A3 A4 B1 B2 B3 B4;
datalines;
1  2  2  4  5  7  8  9
4  6  7  8  6  9  10 11
;
run;
data want;
  set have;
  a=sum(of a:);
  b=sum(of b:);
run;
&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Dec 2016 12:27:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-sum-amp-create-new-variables/m-p/320198#M270981</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-12-20T12:27:37Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro sum &amp; create new variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-sum-amp-create-new-variables/m-p/320201#M270982</link>
      <description>&lt;P&gt;I think I should have described my data a little more:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ADD_CHNG_Q1&lt;/TD&gt;&lt;TD&gt;ADD_CHNG_Q2&lt;/TD&gt;&lt;TD&gt;ADD_CHNG_Q3&lt;/TD&gt;&lt;TD&gt;ADD_CHNG_Q4&lt;/TD&gt;&lt;TD&gt;ADDITIONAL_INFO_Q1&lt;/TD&gt;&lt;TD&gt;ADDITIONAL_INFO_Q2&lt;/TD&gt;&lt;TD&gt;ADDITIONAL_INFO_Q3&lt;/TD&gt;&lt;TD&gt;ADDITIONAL_INFO_Q4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;40&lt;/TD&gt;&lt;TD&gt;55&lt;/TD&gt;&lt;TD&gt;90&lt;/TD&gt;&lt;TD&gt;89&lt;/TD&gt;&lt;TD&gt;70&lt;/TD&gt;&lt;TD&gt;61&lt;/TD&gt;&lt;TD&gt;62&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;16&lt;/TD&gt;&lt;TD&gt;16&lt;/TD&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;TD&gt;28&lt;/TD&gt;&lt;TD&gt;34&lt;/TD&gt;&lt;TD&gt;40&lt;/TD&gt;&lt;TD&gt;46&lt;/TD&gt;&lt;TD&gt;52&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;34&lt;/TD&gt;&lt;TD&gt;21&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;31&lt;/TD&gt;&lt;TD&gt;42&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;28&lt;/TD&gt;&lt;TD&gt;32&lt;/TD&gt;&lt;TD&gt;44&lt;/TD&gt;&lt;TD&gt;96&lt;/TD&gt;&lt;TD&gt;76&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;16&lt;/TD&gt;&lt;TD&gt;32&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have around 800 variables each having a suffix running 1 through 4 &amp;amp; need to sum them .Like Add_Chng_tot=sum(add_chng_q1-&lt;SPAN&gt;add_chng_q4) &amp;amp; so on ...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I could have done the following:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%macro get2(var1,var2,var3,var4,var5);&lt;BR /&gt;data get;&lt;BR /&gt;set get;&lt;BR /&gt;&amp;amp;var5=&amp;amp;var1+&amp;amp;var2+&amp;amp;var3+&amp;amp;var4;&lt;BR /&gt;run;&lt;BR /&gt;%mend get2;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%get2(ADD_CHNG_Q1,ADD_CHNG_Q2,ADD_CHNG_Q3,ADD_CHNG_Q4,ADD_CHNG_Tot)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;But since I have many variables so it is a task to type in the name of the new variable each time (whic is var5 here)&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, 20 Dec 2016 12:43:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-sum-amp-create-new-variables/m-p/320201#M270982</guid>
      <dc:creator>Lopa2016</dc:creator>
      <dc:date>2016-12-20T12:43:59Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro sum &amp; create new variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-sum-amp-create-new-variables/m-p/320202#M270983</link>
      <description>&lt;P&gt;Your problem is a good example of bad data modelling. &amp;nbsp;If you setup your data using long format rather than wide (yes you can, output does not need to match programming use) then your problem becomes moot:&lt;/P&gt;
&lt;PRE&gt;data have;
  input id add_chng_q1	add_chng_q2	add_chng_q3	add_chng_q4	additional_info_q1	additional_info_q2	additional_info_q3	additional_info_q4;
datalines;
1 10	40	55	90	89	70	61	62
2 16	16	22	28	34	40	46	52
;
run;

proc transpose data=have out=inter;
  by id;
  var add_chng_q1--add_chng_q4;
run;

proc sql;
  create table WANT as
  select  ID,
          _NAME_,
          sum(COL1) as RESULT
  from    WORK.INTER
  group by ID,
           _NAME_;
quit;&lt;/PRE&gt;
&lt;P&gt;You can transpose back up if necessary, merge the results back to the data etc. &amp;nbsp;I tend to work with normalised datasets 99% of the time, far easier to work with. &amp;nbsp;If output - either report or data - needs to be transposed, then do that step at the end of the process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, you could do a macro like this:&lt;/P&gt;
&lt;PRE&gt;data have;
  input add_chng_q1	add_chng_q2	add_chng_q3	add_chng_q4	additional_info_q1	additional_info_q2	additional_info_q3	additional_info_q4;
datalines;
10	40	55	90	89	70	61	62
16	16	22	28	34	40	46	52
;
run;

options mlogic mprint symbolgen;
%macro SumThem (var=);
  data have;
    set have;
    &amp;amp;var.=sum(of &amp;amp;var.:);
  run;
%mend SumThem;

%SumThem (var=add_chng_q);&lt;/PRE&gt;
&lt;P&gt;Then you could specify each variable set or you could pull a list out directly from sashelp.vcolumns.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Dec 2016 13:00:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-sum-amp-create-new-variables/m-p/320202#M270983</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-12-20T13:00:08Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro sum &amp; create new variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-sum-amp-create-new-variables/m-p/320204#M270984</link>
      <description>This works perfect .Thanks !! Its a huge data set &amp;amp; transposing was unfortunately not an ideal solution.</description>
      <pubDate>Tue, 20 Dec 2016 13:15:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-sum-amp-create-new-variables/m-p/320204#M270984</guid>
      <dc:creator>Lopa2016</dc:creator>
      <dc:date>2016-12-20T13:15:22Z</dc:date>
    </item>
  </channel>
</rss>

