<?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: PROC APPEND in a loop - how to get the means of the appended data? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628615#M185782</link>
    <description>&lt;P&gt;I have an example of appending with reporting here, including the PROC MEANS portion.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/353374a5d8ea4f0c89ce5d80a47f4a4c" target="_blank"&gt;https://gist.github.com/statgeek/353374a5d8ea4f0c89ce5d80a47f4a4c&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 02 Mar 2020 00:12:58 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2020-03-02T00:12:58Z</dc:date>
    <item>
      <title>PROC APPEND in a loop - how to get the means of the appended data?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628610#M185780</link>
      <description>&lt;P&gt;So let's say I have a macro that loops through 2 iterations, and at the end of each iteration my output is of the form:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Iteration 1 output:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Example;
   input Variable $ Var1 Var2 Var3;
   datalines;
Mean 195  163   100
Max 220  198   240
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Iteration 2 output:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Example2;
   input Variable $ Var1 Var2 Var3;
   datalines;
Mean 25  125   500
Max 210  141   630
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Inside, my macro looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC datasets lib = work nolist; DELETE Example;
%MACRO output_table(y);
/* 
bla bla bla insert code
*/
PROC MEANS DATA = Example;
        var Var1 Var2 Var3;
        output out = Example&amp;amp;y;
QUIT;

PROC append base = Example data = Example&amp;amp;y;
%MEND output_table;

%MACRO iter_1_2;
    %Do y = 1 %TO 2;
       %output_table(&amp;amp;y);
    %End;
%MEND iter_1_2;

RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My end result is just the tables concatenated, for instance:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Example_append;
   input Variable $ Var1 Var2 Var3;
   datalines;
Mean 195  163   100
Max 220  198   240
Mean 25  125   500
Max 210  141   630
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But what I really want is the summarized values of all the variables from all the tables. So in this case:&lt;/P&gt;&lt;P&gt;For the Mean of Var1-Var3, I'd have: (195+25)/2 = 110, (163+125)/2=144, (100+500)/2 = 300&lt;/P&gt;&lt;P&gt;and for max, I'd have: max(220,210)=220, max(198,141)=198, max(240,630)=630 and my output would look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Example_append;
   input Variable $ Var1 Var2 Var3;
   datalines;
Mean 110 144   300
Max  220 198   630
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm unsure what I'm doing wrong and how to get there. Is it a PROC means step on my appended data, after a transpose? I can't transpose them because of the repeat Vars in the rows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is appreciated. Apologies for any typos.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Mar 2020 03:53:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628610#M185780</guid>
      <dc:creator>UniversitySas</dc:creator>
      <dc:date>2020-03-02T03:53:40Z</dc:date>
    </item>
    <item>
      <title>Re: PROC APPEND in a loop - how to get the means of the appended data?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628614#M185781</link>
      <description>I'm confused. Do you have working code that works on a single loop that gives you the results or are you trying to modify the PROC APPEND only. AFAIK that isn't the default output from PROC MEANS that you've shown and you also don't have the Example data set since you've deleted it (first line of code) and you have no way of identifying the output from each loop independently to transpose. &lt;BR /&gt;&lt;BR /&gt;I get that you've likely tried to simplify the code for the forum but its not reflective of whatever issue you're having and it's not clear what the issue is. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 02 Mar 2020 00:11:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628614#M185781</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-03-02T00:11:17Z</dc:date>
    </item>
    <item>
      <title>Re: PROC APPEND in a loop - how to get the means of the appended data?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628615#M185782</link>
      <description>&lt;P&gt;I have an example of appending with reporting here, including the PROC MEANS portion.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/353374a5d8ea4f0c89ce5d80a47f4a4c" target="_blank"&gt;https://gist.github.com/statgeek/353374a5d8ea4f0c89ce5d80a47f4a4c&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Mar 2020 00:12:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628615#M185782</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-03-02T00:12:58Z</dc:date>
    </item>
    <item>
      <title>Re: PROC APPEND in a loop - how to get the means of the appended data?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628654#M185801</link>
      <description>&lt;P&gt;Yes that's correct. My first loop comes out correctly. I understand the PROC means output also includes N, MIN, MAX, MEAN, STDEV, but I omitted it for simplicity.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The gist of what I am trying to do is once the loop is completed, the final table is summarized for each variable, within the same row. So if every iteration outputs a "Max" and "Mean" row, with the columns as Var1-Var3, then the #rows = 2 x #loops (1 loops = 2 rows, 2 loops = 4 rows etc), I instead want it so that after all my loops I only have 2 rows of data, regardless of loops. All the data are summarized in those two rows.&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, 02 Mar 2020 03:58:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628654#M185801</guid>
      <dc:creator>UniversitySas</dc:creator>
      <dc:date>2020-03-02T03:58:08Z</dc:date>
    </item>
    <item>
      <title>Re: PROC APPEND in a loop - how to get the means of the appended data?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628655#M185802</link>
      <description>Thanks! I'll check it out and see if it answers my question.</description>
      <pubDate>Mon, 02 Mar 2020 03:57:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628655#M185802</guid>
      <dc:creator>UniversitySas</dc:creator>
      <dc:date>2020-03-02T03:57:11Z</dc:date>
    </item>
    <item>
      <title>Re: PROC APPEND in a loop - how to get the means of the appended data?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628824#M185874</link>
      <description>&lt;P&gt;I'm not following, how are you planning to collapse those rows? You haven't shown any desired output.&lt;/P&gt;
&lt;P&gt;Are you then trying to take the average of averages? That's generally not a good idea at all and you haven't specified the logic of how you'd want to collapse them.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you wanted to do average of averages, I would recommend factoring in the size of each group at least since a group with N=5 will have a different distrubiton than N=100 and considering them equivalent would be unfair.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=appended MEAN; 
class STAT;
var var1-var3;&lt;BR /&gt;output out=want mean=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/95638"&gt;@UniversitySas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The gist of what I am trying to do is once the loop is completed, the final table is summarized for each variable, within the same row. So if every iteration outputs a "Max" and "Mean" row, with the columns as Var1-Var3, then the #rows = 2 x #loops (1 loops = 2 rows, 2 loops = 4 rows etc), &lt;STRONG&gt;I instead want it so that after all my loops I only have 2 rows of data, regardless of loops. All the data are summarized in those two rows.&amp;nbsp;&lt;/STRONG&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;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Mar 2020 16:37:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628824#M185874</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-03-02T16:37:33Z</dc:date>
    </item>
    <item>
      <title>Re: PROC APPEND in a loop - how to get the means of the appended data?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628899#M185899</link>
      <description>&lt;P&gt;I was hoping to get an output like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Wanted_output;
   input Variable $ Var1 Var2 Var3;
   datalines;
Mean 110 144   300
Max  220 198   630
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Where the mean corresponds to the means across all the different loops, similarly the max is now the max of all the different groups.&lt;/P&gt;&lt;P&gt;Is PROC Append not ideal for solving this?&lt;/P&gt;</description>
      <pubDate>Mon, 02 Mar 2020 20:05:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628899#M185899</guid>
      <dc:creator>UniversitySas</dc:creator>
      <dc:date>2020-03-02T20:05:05Z</dc:date>
    </item>
    <item>
      <title>Re: PROC APPEND in a loop - how to get the means of the appended data?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628909#M185900</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/95638"&gt;@UniversitySas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I was hoping to get an output like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Wanted_output;
   input Variable $ Var1 Var2 Var3;
   datalines;
Mean 110 144   300
Max  220 198   630
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Where the mean corresponds to the means across all the different loops, similarly the max is now the max of all the different groups.&lt;/P&gt;
&lt;P&gt;Is PROC Append not ideal for solving this?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not a loop like this pseudo-code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do i = 1 %to 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;PROC APPEND&lt;/P&gt;
&lt;P&gt;%end&lt;/P&gt;
&lt;P&gt;PROC MEANS&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This PROC MEANS gets you the final output you said you wanted, mean of all groups, max of all groups&lt;/P&gt;</description>
      <pubDate>Mon, 02 Mar 2020 20:24:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628909#M185900</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-03-02T20:24:37Z</dc:date>
    </item>
    <item>
      <title>Re: PROC APPEND in a loop - how to get the means of the appended data?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628917#M185902</link>
      <description>You can take the max of maximums to get an overall max, but you cannot take the mean of means to get your overall mean. So you need to be extra clear about how you want this calculations to happen.</description>
      <pubDate>Mon, 02 Mar 2020 20:33:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628917#M185902</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-03-02T20:33:27Z</dc:date>
    </item>
    <item>
      <title>Re: PROC APPEND in a loop - how to get the means of the appended data?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628955#M185913</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; makes a good point. At the start of this thread, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/95638"&gt;@UniversitySas&lt;/a&gt;, you were very clear that you wanted the mean of the means; but now you seem to be talking about the mean of the data, which is not necessarily the same thing. So we need a lot more clarity here; and could you please discuss the calculations needed without confusing the issue by talking about some looping algorithm, as the algorithm in code should be thought of independently from the desired results.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Mar 2020 23:16:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628955#M185913</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-03-02T23:16:35Z</dc:date>
    </item>
    <item>
      <title>Re: PROC APPEND in a loop - how to get the means of the appended data?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628980#M185928</link>
      <description>&lt;P&gt;Just an update - I managed to solve my problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The issue in my code was here:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC MEANS DATA = Example;
        var Var1 Var2 Var3;
        output out = Example&amp;amp;y;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I did instead was:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC datasets ; DELETE temp;
%MACRO output_table(y);
/* 
bla bla bla insert code
*/
PROC MEANS DATA = Example;
        var Var1 Var2 Var3;
        output out = Example;
QUIT;

PROC append base = temp data = Example;
%MEND output_table;

%MACRO iter_1_2;
    %Do y = 1 %TO 2;
       %output_table(&amp;amp;y);
    %End;
%MEND iter_1_2;

%iter_1_2;

PROC MEANS DATA = temp;
      var Var1 Var2 Var3;
RUN;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This seems to work!&lt;BR /&gt;&lt;BR /&gt;Thanks again for all your help, sorry for the confusion in what I was trying to do.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Mar 2020 00:27:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628980#M185928</guid>
      <dc:creator>UniversitySas</dc:creator>
      <dc:date>2020-03-03T00:27:37Z</dc:date>
    </item>
    <item>
      <title>Re: PROC APPEND in a loop - how to get the means of the appended data?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628990#M185934</link>
      <description>&lt;P&gt;Now let me say I don't think looping is really necessary, and that you shouldn't automatically decide that a loopong solution is necessary. It seems to me that you could have used BY statements to accomplish this, and there would be no need for looping or PROC APPEND, and the code is a lot easier to get right. &lt;/P&gt;
&lt;DIV id="tap-translate"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV id="tap-translate"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Tue, 03 Mar 2020 01:22:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628990#M185934</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-03-03T01:22:59Z</dc:date>
    </item>
    <item>
      <title>Re: PROC APPEND in a loop - how to get the means of the appended data?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628998#M185940</link>
      <description>As long as you're aware that the mean you've calculated doesn't have a lot of meaning you're good I guess. It's not the mean of the data over the set of datasets, it's the average of the averages which has no definition that I'm aware of. If by luck, each data set has the exact same number of records you're good.</description>
      <pubDate>Tue, 03 Mar 2020 02:09:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-APPEND-in-a-loop-how-to-get-the-means-of-the-appended-data/m-p/628998#M185940</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-03-03T02:09:18Z</dc:date>
    </item>
  </channel>
</rss>

