<?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: Calculating the mean for the restricted sample using data statement in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385374#M65747</link>
    <description>&lt;P&gt;You should also be aware of the procedures designed for such:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc summary data final_claims nway;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; class PC4 year;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; where tag_sample=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; var x;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; output out=want&amp;nbsp; mean=x_mean n=x_n;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that without the nway you can get summaries for all the data meeting the where clause, PC4 only, Year only in addition to the combinations of PC4 and year. The automatic variable _type_ is used to indicate which combination the resulting record represents.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also if you have many variables you can place them all on the var statement and use Mean= n= std= /autoname to create output variables with the statistic added to the variable name or even specific statistics for only some of the variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 03 Aug 2017 16:07:56 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-08-03T16:07:56Z</dc:date>
    <item>
      <title>Calculating the mean for the restricted sample using data statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385248#M65730</link>
      <description>&lt;P class="p1"&gt;Hello All,&lt;/P&gt;&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;1. I would like to calculate the mean for a restricted sample of Final_claims where tag_sample=1. &amp;nbsp;How would I build this into the following code?&lt;/P&gt;&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;2. Also, I would like to create a variable N_mean that records the number of observations (per PC4 and year) that were used in the above calculation of the mean.&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="s1"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt;&amp;nbsp;final_claims;&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="s3"&gt;set&lt;/SPAN&gt;&amp;nbsp;final_claims;&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="s3"&gt;by&lt;/SPAN&gt; PC4 year;&lt;/P&gt;&lt;P class="p2"&gt;y=mean(x);&lt;/P&gt;&lt;P class="p4"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN class="s4"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p4"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p4"&gt;&lt;SPAN class="s4"&gt;Thanks&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p4"&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 11:09:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385248#M65730</guid>
      <dc:creator>GKati</dc:creator>
      <dc:date>2017-08-03T11:09:03Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the mean for the restricted sample using data statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385253#M65731</link>
      <description>&lt;P&gt;Such is best done in SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select
  PC4, year,
  avg(x) as y,
  count(*) as n_mean
from final_claims
where tag_sample = 1
group by PC4, year
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Edit: moved the where condition up.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 12:42:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385253#M65731</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-08-03T12:42:02Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the mean for the restricted sample using data statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385254#M65732</link>
      <description>&lt;P&gt;Please use the proc sql if you want to get the mean in this case&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data final_claims;

set final_claims;

by PC4 year;

y=mean(x);

run;

proc sql;
create table final_claims2 as select * , mean(x) as y from final_claims group by PC4, year;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Aug 2017 11:52:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385254#M65732</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2017-08-03T11:52:51Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the mean for the restricted sample using data statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385261#M65734</link>
      <description>&lt;P&gt;1. &amp;nbsp;When running KurtBremser's code&amp;nbsp;I get the following error message after the "where"-statement &amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;sql&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
create &lt;SPAN class="token statement"&gt;table&lt;/SPAN&gt; want as
&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt;
  PC4&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;year&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;
  avg&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;x&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; as y&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;
  &lt;SPAN class="token function"&gt;count&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; as n_mean
&lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; final_claims
&lt;SPAN class="token statement"&gt;where&lt;/SPAN&gt; tag_sample &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;group&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; PC4&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;year&lt;/SPAN&gt;
&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;quit&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P class="p2"&gt;ERROR 22-322: Syntax error, expecting one of the following: ;, !, !!, &amp;amp;, (, *, **, +, ',', -, '.', /, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;, =, &amp;gt;, &amp;gt;=, ?, AND,&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;BETWEEN, CONTAINS, EQ, EQT, EXCEPT, GE, GET, GT, GTT, HAVING, IN, INTERSECT, IS, LE, LET, LIKE, LT, LTT, NE, NET,&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;NOT, NOTIN, OR, ORDER, OUTER, UNION, ^, ^=, |, ||, ~, ~=.&amp;nbsp;&lt;/P&gt;&lt;P class="p2"&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p2"&gt;2. Also, I was hoping to get a VARIABLE with these values. This seems to refer to the separate table. Is it possible to directly calculate these as a variable?&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 13:06:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385261#M65734</guid>
      <dc:creator>GKati</dc:creator>
      <dc:date>2017-08-03T13:06:40Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the mean for the restricted sample using data statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385279#M65737</link>
      <description>&lt;P&gt;Ups. Please move the where condition up one line, before the group by.&lt;/P&gt;
&lt;P&gt;I edited my post accordingly.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 13:16:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385279#M65737</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-08-03T13:16:10Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the mean for the restricted sample using data statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385291#M65740</link>
      <description>&lt;P&gt;If you want your stat re-merged, do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select
  *,
  avg(x) as y,
  count(*) as n_mean
from final_claims
where tag_sample = 1
group by PC4, year
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I advise against overwriting a dataset in the same step. If something happens, you need to recreate the dataset.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 13:33:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385291#M65740</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-08-03T13:33:27Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the mean for the restricted sample using data statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385374#M65747</link>
      <description>&lt;P&gt;You should also be aware of the procedures designed for such:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc summary data final_claims nway;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; class PC4 year;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; where tag_sample=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; var x;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; output out=want&amp;nbsp; mean=x_mean n=x_n;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that without the nway you can get summaries for all the data meeting the where clause, PC4 only, Year only in addition to the combinations of PC4 and year. The automatic variable _type_ is used to indicate which combination the resulting record represents.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also if you have many variables you can place them all on the var statement and use Mean= n= std= /autoname to create output variables with the statistic added to the variable name or even specific statistics for only some of the variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 16:07:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385374#M65747</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-08-03T16:07:56Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the mean for the restricted sample using data statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385636#M65758</link>
      <description>&lt;P&gt;&lt;SPAN class="login-bold"&gt;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562" target="_self"&gt;KurtBremser&lt;/A&gt;:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;It seems to me that all observations where tag_sample is not equal to 1 get dropped from "final_claims". Therefore i would need to re-merge y and n_mean into my original dataset if I wanted them together with all observations. Isn't there version of this code where those observations don't get dropped, i.e. no merging is needed?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;Thanks&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Aug 2017 12:58:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385636#M65758</guid>
      <dc:creator>GKati</dc:creator>
      <dc:date>2017-08-04T12:58:13Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the mean for the restricted sample using data statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385670#M65761</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select
  a.*,
  avg(x) as y,
  count(*) as n_mean
from final_claims a
where tag_sample = 1
group by PC4, year
union all
select
  b.*,
  0 as y,
  0 as n_mean
from final_claims b
where tag_sample ne 1
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Aug 2017 14:43:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculating-the-mean-for-the-restricted-sample-using-data/m-p/385670#M65761</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-08-04T14:43:51Z</dc:date>
    </item>
  </channel>
</rss>

