<?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: Simplest way to create a variable equal to a column's grand total in a data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Simplest-way-to-create-a-variable-equal-to-a-column-s-grand/m-p/838458#M331514</link>
    <description>&lt;P&gt;A data step approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   if _N_ = 1 then do;
      do until (z);
         set sashelp.class end = z;
         total_age = sum(total_age, age);
         retain total_age;
      end;
   end;

   set sashelp.class;
   
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 13 Oct 2022 17:53:42 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2022-10-13T17:53:42Z</dc:date>
    <item>
      <title>Simplest way to create a variable equal to a column's grand total in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplest-way-to-create-a-variable-equal-to-a-column-s-grand/m-p/838448#M331509</link>
      <description>&lt;P&gt;I'm wondering what's the best way to create a variable that contains a column's total sum inside of a data step. Can this be easily achieved without proc sql?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As an example, I'd like to create a new variable, total_age, that contains the sum of all the students' ages, in the sashelp.class dataset. My code below creates a cumulative sum, but how would I revise to create a total so that each row displays the total age? Thank you.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data sas_intro;
set sashelp.class;
retain;
total_age = sum(total_age, age); /*creates cumulative sum, not total)*/
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Oct 2022 17:03:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplest-way-to-create-a-variable-equal-to-a-column-s-grand/m-p/838448#M331509</guid>
      <dc:creator>everyone</dc:creator>
      <dc:date>2022-10-13T17:03:00Z</dc:date>
    </item>
    <item>
      <title>Re: Simplest way to create a variable equal to a column's grand total in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplest-way-to-create-a-variable-equal-to-a-column-s-grand/m-p/838452#M331512</link>
      <description>&lt;P&gt;It's either two steps in a data step (calculation + merge) or a DoW loop. Programmatically, SQL is much easier here for sure. &lt;BR /&gt;&lt;BR /&gt;Depending on what you're doing, for example creating a report, the reporting procedure may be better suited to doing that as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sas_intro;
do _n_ =1 by 1 until(eof);
set sashelp.class end=eof;
total_age = sum(total_age, age); /*creates cumulative sum, not total)*/
end;

do _n_ =1 by 1 until(eof2);
set sashelp.class end=eof2;
output;
end;


run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Other methods illustrated here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Oct 2022 17:34:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplest-way-to-create-a-variable-equal-to-a-column-s-grand/m-p/838452#M331512</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-10-13T17:34:29Z</dc:date>
    </item>
    <item>
      <title>Re: Simplest way to create a variable equal to a column's grand total in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplest-way-to-create-a-variable-equal-to-a-column-s-grand/m-p/838457#M331513</link>
      <description>&lt;P&gt;An SQL approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   create table want as
   select *
        , sum(age) as total_age 
   from sashelp.class
   ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Oct 2022 17:50:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplest-way-to-create-a-variable-equal-to-a-column-s-grand/m-p/838457#M331513</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-10-13T17:50:39Z</dc:date>
    </item>
    <item>
      <title>Re: Simplest way to create a variable equal to a column's grand total in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplest-way-to-create-a-variable-equal-to-a-column-s-grand/m-p/838458#M331514</link>
      <description>&lt;P&gt;A data step approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   if _N_ = 1 then do;
      do until (z);
         set sashelp.class end = z;
         total_age = sum(total_age, age);
         retain total_age;
      end;
   end;

   set sashelp.class;
   
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Oct 2022 17:53:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplest-way-to-create-a-variable-equal-to-a-column-s-grand/m-p/838458#M331514</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-10-13T17:53:42Z</dc:date>
    </item>
  </channel>
</rss>

