全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
13107 33
2010-02-26
在下是 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!
二维码

扫码加我 拉你入群

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

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

全部回复
2010-2-26 00:31:46
sorry i cannot  help more but tell you that finding a textbook wound be the best way
二维码

扫码加我 拉你入群

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

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

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
是不是写错了?
二维码

扫码加我 拉你入群

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

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

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

是不是应该这样呢?
二维码

扫码加我 拉你入群

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

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

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
二维码

扫码加我 拉你入群

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

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

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
二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群