楼主: zenith1107
13243 33

[原创博文] SAS 计算平均值问题 [推广有奖]

11
frackdeng 发表于 2010-2-26 09:20:30
不对啊,和lz的要求不一样。

12
frackdeng 发表于 2010-2-26 09:47:15
  1. proc sql;
  2.     create table test1 as
  3.         select *,sum(price) as sum1,count(*) as count1
  4.         from test
  5.         group by week,day
  6.         order by price,week,day
  7.         ;
  8.         create table test2 as
  9.         select *,sum(price) as sum2,count(*) as count2
  10.         from test
  11.         group by day
  12.         order by price,week,day;
  13. run;

  14. data test3;
  15.    merge test1 test2;
  16.    by  price week day;
  17.    meanp=(sum2-sum1)/(count2-count1);
  18.    keep week day price meanp;
  19. run;
复制代码

13
soporaeternus 发表于 2010-2-26 11:11:07
  1. proc sql;
  2.         create table t1 as
  3.                 select
  4.                                         a.Week
  5.                                         ,a.Day
  6.                                         ,a.Price
  7.                                         ,mean(b.Price) as MeanP
  8.                 from                test a
  9.                 inner join        test b
  10.                 on                        a.Day=b.Day
  11.                                 and a.Week^=b.Week
  12.                 group by        a.Week
  13.                                         ,a.Day
  14.                                         ,a.Price
  15.                 order by        a.Day
  16.                                         ,a.Week
  17.         ;
  18. quit;
复制代码
试试......
Let them be hard, but never unjust

14
frackdeng 发表于 2010-2-26 12:29:03
学习了,厉害!13# soporaeternus

15
jingju11 发表于 2010-2-26 22:21:29
zenith1107 发表于 2010-2-26 00:07
在下是 SAS  新手,请多指教。

DATA test;
input Week $ Day Price;
CARDS;
111 1 1
111 1 2
112 1 3
112 1 4
111 2 5
111 2 6
112 2 7
112 2 8
;

I want to create a new variable MEANP that stores the mean PRICE for the observations in the same DAY but different WEEK. For example, for the 1st and 2nd row, the value of MEANP both = the mean PRICE of the rows where WEEK=112 and DAY=1 (the 3rd and 4th rows) and for the 3rd and 4th row, MEANP both = the mean PRICE of the rows where WEEK=111 and DAY=1 (the 1st and 2nd rows). And the final result would look like this:

WEEK DAY PRICE MEANP
111 1 1 3.5
111 1 2 3.5
112 1 3 1.5
112 1 4 1.5
111 2 5 7.5
111 2 6 7.5
112 2 7 5.5
112 2 8 5.5

I'm wondering if there's an easy way to do that. Any help is greatly appreciated.

Thanks!

  1. proc sort; by day week; run;
复制代码
sorry, no better code……JingJu……

16
ademons 发表于 2010-2-26 23:02:56
Just for fun: avoid using proc sql……JingJu……
-------------------
真受不了。
已有 1 人评分信用等级 收起 理由
jingju11 + 1 我很赞同

总评分: 信用等级 + 1   查看全部评分

17
diamont 发表于 2010-2-27 15:11:58
KnowBie , good job
I like it !

18
zenith1107 发表于 2010-2-28 10:41:26
自己想出来了,不过多谢各位!!

DATA test;
input BRAND $ Week Price;
CARDS;
111 1 1
111 1 2
112 1 3
112 1 4
111 2 5
111 2 6
112 2 7
112 2 8
;

RUN;

PROC SQL;
create table test2 as
select *,mean(price) as total_mean, count(price) as count_price from test
group by WEEK;
QUIT;

PROC SQL;
create table test3 as
select *,mean(price) as total_mean2, count(price) as count_price2 from test
group by WEEK, BRAND;
QUIT;

DATA test4;
MERGE test test2 test3;
BY BRAND;
total2=total_mean2*count_price2;
comptotal=total-total2;
compcount=count_price-count_price2;
compavg=comptotal/compcount;
RUN;

19
crackman 发表于 2010-2-28 11:57:42
学习的好机会
谢谢bobguy  gzjb

20
crackman 发表于 2010-2-28 12:07:18
8# bobguy
     2010年02月28日 星期日 上午11时55分36秒   
                                             Week           Day     Price  avgprice
                                             --------------------------------------
                                             111              1         2       1.5
                                             111              1         1       1.5
                                             112              1         3       3.5
                                             112              1         4       3.5
                                             111              2         6       5.5
                                             111              2         5       5.5
                                             112              2         7       7.5
                                             112              2         8       7.5
好像和要求有点不一样哦?

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-26 19:04