<?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: Average mean calculation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336589#M76313</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Calculating the mean with and without missing values

You can cut and paste the R code into IML/R or
you can just use IML.

HAVE
====

Up to 40 obs from sd1.have total obs=6

Obs    PRICE    SHARE

 1       1        1
 2       2        3
 3       3        .
 4       3        .
 5       2        1
 6       1        3

WANT
====

  AVG_PRICE=2.00
  AVG_SHARE=2.00

BUT I GET
=========

  AVG_PRICE=2.00
  AVG_SHARE=.

WORKING CODE FOR ADJUSTING FOR MISSINGS
=======================================

   R
   colMeans(have, na.rm=TRUE);  (adjust for missing)
   colMeans(have, na.rm=FALSE); (do not adjust for missing)

  SAS/WPS
   tot2=sum(tot2,share);
   cnt2=ifn(share ne .,cnt2+1,cnt2);

FULL SOLUTION
=============

* create some data;
options validvarname=upcase;
libname sd1 "d:/sd1";
data sd1.have(drop=code);
input price share;
cards4;
1 1
2 3
3 .
3 .
2 1
1 3
;;;;
run;quit;

* DO NOT ADJUST FOR MISSING;
data want;
   set sd1.have end = eof;
   cumprice = cumprice + price;
   cumshare = cumshare + share;

   if eof = 1 then do;
      meanprice = cumprice / _N_;
      meanshare = cumshare / _N_;
   end;

   retain cumprice cumshare 0;
   drop cumprice cumshare;
run;

* ADJUST FOR MISSING;
%utl_submit_wps64('
libname sd1 "d:/sd1";
data _NULL_;
 set sd1.have end=eof;
      retain tot1 tot2 cnt1 cnt2 0;
      tot1=sum(tot1,price);
      tot2=sum(tot2,share);
      cnt1=ifn(price ne .,cnt1+1,cnt1);
      cnt2=ifn(share ne .,cnt2+1,cnt2);
    if eof then do;
        avg_price  = tot1/cnt1;
        avg_share = tot2/cnt2;
        put avg_price= avg_share=;
    end;
   format avg_price avg_share 5.2;
run;
');

* R SOLUTION;
%utl_submit_wps64('
  options set=R_HOME "C:/Program Files/R/R-3.3.1";
  proc r;
  submit;
  library(haven);
  have=read_sas("d:/sd1/have.sas7bdat");
  have;
  colMeans(have, na.rm=TRUE);
  colMeans(have, na.rm=FALSE);
  endsubmit;
');

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 28 Feb 2017 15:06:10 GMT</pubDate>
    <dc:creator>rogerjdeangelis</dc:creator>
    <dc:date>2017-02-28T15:06:10Z</dc:date>
    <item>
      <title>Average mean calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336482#M76286</link>
      <description>&lt;P&gt;I have the following data&lt;BR /&gt;code price share&lt;BR /&gt;------------------&lt;BR /&gt;AMGN 67.66 100&lt;BR /&gt;DELL 24.60 200&lt;BR /&gt;GE 34.50 100&lt;BR /&gt;HPQ 32.32 120&lt;BR /&gt;IBM 82.25 50&lt;BR /&gt;MOT 30.24 100&lt;/P&gt;&lt;P&gt;I need to calculate the average of the price and share in data step without using procedures&lt;/P&gt;&lt;P&gt;AMGN 67.66 100&lt;BR /&gt;DELL 24.60 200&lt;BR /&gt;GE 34.50 100&lt;BR /&gt;HPQ 32.32 120&lt;BR /&gt;IBM 82.25 50&lt;BR /&gt;MOT 30.24 100&lt;BR /&gt;45.26 111.6&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2017 09:36:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336482#M76286</guid>
      <dc:creator>molla</dc:creator>
      <dc:date>2017-02-28T09:36:20Z</dc:date>
    </item>
    <item>
      <title>Re: Average mean calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336486#M76288</link>
      <description>&lt;P&gt;Is this another "interview" question of which we are seeing several recently? &amp;nbsp;If so its ridiculous, perhaps you should not use your fingers to type in, or use words to communicate. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;As for datastep:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have end=last;
  retain cnt tot_price tot_share;
  cnt=sum(cnt,1);
  tot_price=sum(tot_price,price);
  tot_share=sum(tot_share,share);
  if last then do;
    mean_price=tot_price/cnt;
    mean_share=tot_share/cnt;
    output;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;However, maybe you would be better not using SAS at all, get yourself an assembler and do it that way?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2017 09:51:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336486#M76288</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-02-28T09:51:14Z</dc:date>
    </item>
    <item>
      <title>Re: Average mean calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336488#M76289</link>
      <description>&lt;P&gt;Like this? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input code $ price share;
infile datalines;
datalines;
AMGN 67.66 100
DELL 24.60 200
GE 34.50 100
HPQ 32.32 120
IBM 82.25 50
MOT 30.24 100
;

data want;
   set have end = eof;
   cumprice = cumprice + price;
   cumshare = cumshare + share;

   if eof = 1 then do;
      meanprice = cumprice / _N_;
      meanshare = cumshare / _N_;
   end;

   retain cumprice cumshare 0;
   drop cumprice cumshare;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Feb 2017 09:51:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336488#M76289</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-02-28T09:51:57Z</dc:date>
    </item>
    <item>
      <title>Re: Average mean calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336489#M76290</link>
      <description>&lt;P&gt;data _NULL_;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;set have end=eof;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; retain tot1 tot2 count;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; tot1+price;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; tot2+share;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; count+;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; if eof then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; avg_price &amp;nbsp;= tot1/count;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;avg_share = tot2/count;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;put avg_price= avg_share=;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;format avg_price avg_share 5.2;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2017 09:53:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336489#M76290</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-02-28T09:53:58Z</dc:date>
    </item>
    <item>
      <title>Re: Average mean calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336535#M76294</link>
      <description>&lt;P&gt;If this is an interview question, I would tell the person that the code should handle missing values. That would separate the wheat from the chaff.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2017 13:19:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336535#M76294</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-02-28T13:19:13Z</dc:date>
    </item>
    <item>
      <title>Re: Average mean calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336589#M76313</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Calculating the mean with and without missing values

You can cut and paste the R code into IML/R or
you can just use IML.

HAVE
====

Up to 40 obs from sd1.have total obs=6

Obs    PRICE    SHARE

 1       1        1
 2       2        3
 3       3        .
 4       3        .
 5       2        1
 6       1        3

WANT
====

  AVG_PRICE=2.00
  AVG_SHARE=2.00

BUT I GET
=========

  AVG_PRICE=2.00
  AVG_SHARE=.

WORKING CODE FOR ADJUSTING FOR MISSINGS
=======================================

   R
   colMeans(have, na.rm=TRUE);  (adjust for missing)
   colMeans(have, na.rm=FALSE); (do not adjust for missing)

  SAS/WPS
   tot2=sum(tot2,share);
   cnt2=ifn(share ne .,cnt2+1,cnt2);

FULL SOLUTION
=============

* create some data;
options validvarname=upcase;
libname sd1 "d:/sd1";
data sd1.have(drop=code);
input price share;
cards4;
1 1
2 3
3 .
3 .
2 1
1 3
;;;;
run;quit;

* DO NOT ADJUST FOR MISSING;
data want;
   set sd1.have end = eof;
   cumprice = cumprice + price;
   cumshare = cumshare + share;

   if eof = 1 then do;
      meanprice = cumprice / _N_;
      meanshare = cumshare / _N_;
   end;

   retain cumprice cumshare 0;
   drop cumprice cumshare;
run;

* ADJUST FOR MISSING;
%utl_submit_wps64('
libname sd1 "d:/sd1";
data _NULL_;
 set sd1.have end=eof;
      retain tot1 tot2 cnt1 cnt2 0;
      tot1=sum(tot1,price);
      tot2=sum(tot2,share);
      cnt1=ifn(price ne .,cnt1+1,cnt1);
      cnt2=ifn(share ne .,cnt2+1,cnt2);
    if eof then do;
        avg_price  = tot1/cnt1;
        avg_share = tot2/cnt2;
        put avg_price= avg_share=;
    end;
   format avg_price avg_share 5.2;
run;
');

* R SOLUTION;
%utl_submit_wps64('
  options set=R_HOME "C:/Program Files/R/R-3.3.1";
  proc r;
  submit;
  library(haven);
  have=read_sas("d:/sd1/have.sas7bdat");
  have;
  colMeans(have, na.rm=TRUE);
  colMeans(have, na.rm=FALSE);
  endsubmit;
');

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Feb 2017 15:06:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336589#M76313</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-02-28T15:06:10Z</dc:date>
    </item>
    <item>
      <title>Re: Average mean calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336595#M76314</link>
      <description>&lt;P&gt;If I didn't want the job very badly I'd give them something like this...&lt;/P&gt;
&lt;PRE&gt;%macro get_means(data=,var=, class=, classval=);
  proc means data=&amp;amp;data.;
    %if not(%sysevalf(%superq(class)=,boolean)) %then %do;
      class &amp;amp;class.;
      ways 1;
    %end;
    var &amp;amp;var.;
    output out=_temp_means mean(&amp;amp;var.)=;
  run;

  data _null_;
    set _temp_means;
    %if not(%sysevalf(%superq(class)=,boolean)) %then %do;
      where &amp;amp;class. = &amp;amp;classval.;
    %end;
    call symputx("mean_&amp;amp;var.",&amp;amp;var.,'g');
  run;

  proc delete data=_temp_means;
  run;
%mend get_means;

data want;
  set sashelp.class;
  rc = dosubl('%get_means(data=sashelp.class,var=height,class=sex,classval="'||sex||'")');
  height_mean = symget('mean_height');
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Feb 2017 15:26:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336595#M76314</guid>
      <dc:creator>snoopy369</dc:creator>
      <dc:date>2017-02-28T15:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: Average mean calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336598#M76317</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/42345"&gt;@rogerjdeangelis&lt;/a&gt;'s code is close, but doesn't handle the boundary case.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2017 15:32:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336598#M76317</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-02-28T15:32:40Z</dc:date>
    </item>
    <item>
      <title>Re: Average mean calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336748#M76367</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Good Point Rosk

Missings are the bane of programming. However  my R code does cover this case.

I suspect this is the case and I need to chck for 0/0 in the SAS code.


# A tibble: 6 × 2
  PRICE SHARE
  &amp;lt;dbl&amp;gt; &amp;lt;dbl&amp;gt;
1     1    NA
2     2    NA
3     3    NA
4     3    NA
5     2    NA
6     1    NA

PRICE SHARE
    2   NaN          ==&amp;gt; 0/0

PRICE SHARE
    2    NA
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Feb 2017 21:38:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-mean-calculation/m-p/336748#M76367</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-02-28T21:38:00Z</dc:date>
    </item>
  </channel>
</rss>

