楼主: zenith1107
13240 33

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

  • 0关注
  • 0粉丝

初中生

28%

还不是VIP/贵宾

-

威望
0
论坛币
4 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
100 点
帖子
8
精华
0
在线时间
16 小时
注册时间
2008-2-23
最后登录
2013-11-12

楼主
zenith1107 发表于 2010-2-26 00:07:07 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
在下是 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!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:平均值 observations observation appreciate Different

回帖推荐

markai 发表于9楼  查看完整内容

超人!!!!!

gzjb 发表于6楼  查看完整内容

/************************************************************************************************** **** Here Just show a complicated way to implement it. If anyone knows a simple way ***** *** Please let me know. Thanks a lot **********************************/; *********************** SAS Code **********************; DATA test; input Week $ Day P ...

本帖被以下文库推荐

沙发
mcfong 发表于 2010-2-26 00:31:46
sorry i cannot  help more but tell you that finding a textbook wound be the best way

藤椅
markai 发表于 2010-2-26 00:58:26
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
是不是写错了?
钉子精神

板凳
markai 发表于 2010-2-26 00:59:18
111 1 1 1.5
111 1 2 1.5
112 1 3 3.5
112 1 4 3.5
111 2 5 5.5
111 2 6 5.5
112 2 7 7.5
112 2 8 7.5

是不是应该这样呢?
钉子精神

报纸
zenith1107 发表于 2010-2-26 02:43:52
没错。对每一个week-day (e.g. week 111, day 1), 我想求出同一day里所有不是这个week的price平均值 (eg week 112, day 1)。

是不是要用 PROC SQL 呢?

谢谢

4# markai

地板
gzjb 发表于 2010-2-26 03:59:18
/**************************************************************************************************
****   Here Just show a complicated way to implement it. If anyone knows a simple way *****
*** Please let me know.  Thanks a lot                                      **********************************/;

***********************  SAS Code **********************;

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
;


proc sort data=test;
by day  week;
run;


proc means data=test;
  by Day Week;
  var price;
  output out=meanout(keep= Week Day priceMean) MEAN(price)=priceMean;
run;


data _NULL_;
set meanout;
File 'C:\ProjStudy\gzjb\interchange.txt';
put Day Week PriceMean;
run;


data meanP;
  infile 'C:\ProjStudy\gzjb\interchange.txt';
  input Day Week1$ PriceMean1@;
  input Day Week2$ PriceMean2;
  run;



  data meanP1 (drop=tmPrice);
    set meanP;
        tmPrice=PriceMean1;
        PriceMean1=PriceMean2;
    PriceMean2=tmPrice;
        run;

        data meanPweek1(keep=Day Week1 PriceMean1)
        meanPweek2(keep=Day Week2 PriceMean2);
          set MeanP1;
        run;

        data mergePweek;
            merge meanPweek1(rename=( Week1=Week PriceMean1=MeanP)) meanPweek2(rename=( Week2=Week PriceMean2=MeanP));
            by Day Week;
    run;

data lastData;
  merge test mergePweek;
  by Day Week;
  run;

proc print noobs; run;

******************************************
SAS Output:
****************************************
                              
                                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
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

7
markai 发表于 2010-2-26 06:47:49
太牛了!!!!!!!!!!!!
钉子精神

8
bobguy 发表于 2010-2-26 07:35:05
gzjb 发表于 2010-2-26 03:59
/**************************************************************************************************
****   Here Just show a complicated way to implement it. If anyone knows a simple way *****
*** Please let me know.  Thanks a lot                                      **********************************/;

***********************  SAS Code **********************;

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
;


proc sort data=test;
by day  week;
run;


proc means data=test;
  by Day Week;
  var price;
  output out=meanout(keep= Week Day priceMean) MEAN(price)=priceMean;
run;


data _NULL_;
set meanout;
File 'C:\ProjStudy\gzjb\interchange.txt';
put Day Week PriceMean;
run;


data meanP;
  infile 'C:\ProjStudy\gzjb\interchange.txt';
  input Day Week1$ PriceMean1@;
  input Day Week2$ PriceMean2;
  run;



  data meanP1 (drop=tmPrice);
    set meanP;
        tmPrice=PriceMean1;
        PriceMean1=PriceMean2;
    PriceMean2=tmPrice;
        run;

        data meanPweek1(keep=Day Week1 PriceMean1)
        meanPweek2(keep=Day Week2 PriceMean2);
          set MeanP1;
        run;

        data mergePweek;
            merge meanPweek1(rename=( Week1=Week PriceMean1=MeanP)) meanPweek2(rename=( Week2=Week PriceMean2=MeanP));
            by Day Week;
    run;

data lastData;
  merge test mergePweek;
  by Day Week;
  run;

proc print noobs; run;

******************************************
SAS Output:
****************************************
                              
                                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
It can be much simplified as,

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
;

proc sql;
  select *, mean(price)    as avgprice
  from test
  group by 1,2
  order by 2,1
  ;
  quit;
已有 1 人评分经验 收起 理由
爱萌 + 20 简洁明了

总评分: 经验 + 20   查看全部评分

9
markai 发表于 2010-2-26 07:38:14
超人!!!!!
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

钉子精神

10
gzjb 发表于 2010-2-26 08:48:02
Thank bobguy.


God bless you and your family

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

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