<?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: calculated row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/909008#M358630</link>
    <description>&lt;P&gt;Transpose.&lt;/P&gt;
&lt;P&gt;Transpose.&lt;/P&gt;
&lt;P&gt;Transpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Having data (months) in structure (variable names) is (almost) always a&amp;nbsp;&lt;STRONG&gt;BAD IDEA&lt;/STRONG&gt;.&lt;/P&gt;</description>
    <pubDate>Wed, 20 Dec 2023 12:59:35 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2023-12-20T12:59:35Z</dc:date>
    <item>
      <title>calculated row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/908577#M358537</link>
      <description>&lt;P&gt;Hi , Three is small help needed&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the below dataset&amp;nbsp;&lt;/P&gt;&lt;P&gt;data sample;&lt;BR /&gt;input name$ june july;&lt;BR /&gt;datalines ;&lt;BR /&gt;amount 170 140&lt;BR /&gt;account 2 4&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;I need to create new row &lt;STRONG&gt;average&lt;/STRONG&gt; which is amount/account&amp;nbsp;&lt;/P&gt;&lt;P&gt;output :&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;name&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;june&lt;/TD&gt;&lt;TD&gt;july&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;amount&lt;/TD&gt;&lt;TD&gt;170&lt;/TD&gt;&lt;TD&gt;140&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;account&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;average&lt;/TD&gt;&lt;TD&gt;85&lt;/TD&gt;&lt;TD&gt;35&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2023 09:57:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/908577#M358537</guid>
      <dc:creator>ss171</dc:creator>
      <dc:date>2023-12-18T09:57:42Z</dc:date>
    </item>
    <item>
      <title>Re: calculated row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/908587#M358538</link>
      <description>&lt;P&gt;you can transpose it for a better structure. If you need it in the old form, you can transpose it back.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data sample;
input name$ june july;
datalines ;
amount 170 140
account 2 4
;
run;

proc transpose data=sample out=long;
id name;
run;

data want;
set long;
avg=amount/account;
rename _NAME_= Month;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Dec 2023 11:04:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/908587#M358538</guid>
      <dc:creator>rudfaden</dc:creator>
      <dc:date>2023-12-18T11:04:11Z</dc:date>
    </item>
    <item>
      <title>Re: calculated row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/908588#M358539</link>
      <description>&lt;P&gt;You are probably better off with a transposed dataset (you may have some other identifying variables on the input data, which can be put in a BY statement):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=sample out=transposed(rename=(_name_=month));
  var june--july;
  id name;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can quite easily calculate the average:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data calc;
  set transposed;
  average=amount/account;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you MUST have things back, just transpose again:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=calc out=want(rename=(_name_=name));
  var amount account average;
  id month;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Dec 2023 11:05:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/908588#M358539</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-12-18T11:05:05Z</dc:date>
    </item>
    <item>
      <title>Re: calculated row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/908593#M358540</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/300897"&gt;@ss171&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi , Three is small help needed&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the below dataset&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data sample;&lt;BR /&gt;input name$ june july;&lt;BR /&gt;datalines ;&lt;BR /&gt;amount 170 140&lt;BR /&gt;account 2 4&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;I need to create new row &lt;STRONG&gt;average&lt;/STRONG&gt; which is amount/account&amp;nbsp;&lt;/P&gt;
&lt;P&gt;output :&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;name&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;june&lt;/TD&gt;
&lt;TD&gt;july&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;amount&lt;/TD&gt;
&lt;TD&gt;170&lt;/TD&gt;
&lt;TD&gt;140&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;account&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;average&lt;/TD&gt;
&lt;TD&gt;85&lt;/TD&gt;
&lt;TD&gt;35&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't understand the question — average of what? Are you trying to average the numbers shown next to amount and next to account? That makes no sense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Furthermore, as others have pointed out, this is a very very poor layout to do anything in SAS. Calendar information (such as "June") should never be stored in variable names. It should be stored as the value of a variable named Month. Furthermore, dates in SAS are best set up as integers representing the number of days since 01JAN1960, and then all of the work SAS has done to properly deal with dates and months and weeks and years is available to you, and you don't have to figure out how to handle dates yourself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input month :date9. amount account;
    format month monyy7.;
    cards;
01MAR2023 170 2
01APR2023 140 4
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do not include in the above code a method of averaging, as you have not explained the averages that you want. However, if you want the average over all months of amount, and the average over all months of account, this will provide the average (which is different than what you show)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=have mean;
    var amount account;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a benefit of having the proper layout of your data, then every SAS PROC is available and will make your calculations easier. The results of PROC MEANS could be appended to your data if that's what you want (I do not show that code as I am still not sure what averages you want).&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2023 12:20:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/908593#M358540</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-12-18T12:20:27Z</dc:date>
    </item>
    <item>
      <title>Re: calculated row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/908752#M358565</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Here is SAS/IML solution.*/
data sample;
input name$ june july;
datalines ;
amount 170 140
account 2 4
;

proc iml;
use sample;
read all var{name};
read all var _num_ into x[c=vname];
close;
name=name//'average';
want=x//(x[1,]/x[2,]);
create want from want[r=name c=vname];
append from want[r=name];
close;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Dec 2023 02:30:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/908752#M358565</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-12-19T02:30:46Z</dc:date>
    </item>
    <item>
      <title>Re: calculated row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/908800#M358576</link>
      <description>&lt;P&gt;Proc SQL solution was pending, so here it is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;proc sql;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; select name as name,&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;june,&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; july&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; from sample&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; union&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; select 'average' as name,&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mean(june) as june,&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; mean(july) as july&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; from sample;&lt;/DIV&gt;
&lt;DIV&gt;quit;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Dec 2023 13:33:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/908800#M358576</guid>
      <dc:creator>Aku</dc:creator>
      <dc:date>2023-12-19T13:33:12Z</dc:date>
    </item>
    <item>
      <title>Re: calculated row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/909005#M358628</link>
      <description>Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13573"&gt;@Paige&lt;/a&gt; Miller, but this is just a dummy data which I have created to understand the way of doing. Actual data is something different only.&lt;BR /&gt;</description>
      <pubDate>Wed, 20 Dec 2023 12:06:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/909005#M358628</guid>
      <dc:creator>ss171</dc:creator>
      <dc:date>2023-12-20T12:06:24Z</dc:date>
    </item>
    <item>
      <title>Re: calculated row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/909007#M358629</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/300897"&gt;@ss171&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13573"&gt;@Paige&lt;/a&gt; Miller, but this is just a dummy data which I have created to understand the way of doing. Actual data is something different only.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Great. As everyone has stated, you have data in a very poor layout for working in SAS. You would find it much easier to transpose the data rather than work with the data in the layout you show.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Dec 2023 14:26:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/909007#M358629</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-12-20T14:26:12Z</dc:date>
    </item>
    <item>
      <title>Re: calculated row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/909008#M358630</link>
      <description>&lt;P&gt;Transpose.&lt;/P&gt;
&lt;P&gt;Transpose.&lt;/P&gt;
&lt;P&gt;Transpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Having data (months) in structure (variable names) is (almost) always a&amp;nbsp;&lt;STRONG&gt;BAD IDEA&lt;/STRONG&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Dec 2023 12:59:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/909008#M358630</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-12-20T12:59:35Z</dc:date>
    </item>
    <item>
      <title>Re: calculated row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/909009#M358631</link>
      <description>&lt;P&gt;How many distinct values of name does your real dataset have? How many months?&lt;/P&gt;</description>
      <pubDate>Wed, 20 Dec 2023 13:00:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/909009#M358631</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-12-20T13:00:49Z</dc:date>
    </item>
    <item>
      <title>Re: calculated row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/909086#M358638</link>
      <description>&lt;P&gt;I generally agree with the conventional wisdom on the advantages of structuring data in a long vs wide format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But in this case - 2 rows (a sum row followed by a frequency row), a set of ID variables (only NAME in this case), and any number of columns, there is really no need for transpose, assuming you really do want an additional row of averages:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
input name$ june july;
datalines ;
amount 170 140
account 2 4
run;

data want;
  set sample  end=end_of_sample;
  output;
  array vals _numeric_;
  do over vals;
    vals=lag(vals)/vals;
  end;
  if end_of_sample then do;
    name='Average';
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will produce the following log note:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      2 at 19:19
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which can be avoided by using&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    vals=sum(0,lag(vals))/vals;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;inside the "DO OVER VALS" loop.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Dec 2023 16:20:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculated-row/m-p/909086#M358638</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-12-20T16:20:58Z</dc:date>
    </item>
  </channel>
</rss>

