Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate conditional MEAN in SAS

Status
Not open for further replies.

zenith1107

Technical User
Feb 25, 2010
2
0
0
US
Hi all,

I'm a new user of SAS and I have encountered a problem:

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!
 
Hi Zenith.

Based on the specific example you have given I came up with this:

Code:
proc sql;                                           
  create table RESULTS as                           
    select a.week, a.day, price, b.meanp            
    from TEST a                                     
    left join                                       
   (select distinct week, day, mean(price) as meanp 
    from TEST                                       
    group by week, day                              
   ) b                                              
    on a.day = b.day and                            
       a.week ne b.week                             
    order by a.day, a.week, price;                  
quit;

Here is my attempt at code which will handle another day and weeks worth of data:

Code:
proc sql;                                                          
  create table results as                                          
    select a.week, a.day, price, b.meanp, sum(b.meanp) as totmeanp 
    from TEST a                                                    
    left join                                                      
   (select distinct week, day, mean(price) as meanp                
    from TEST                                                      
    group by week, day                                             
   ) b                                                             
    on a.day = b.day and                                           
       a.week ne b.week                                            
    group by a.week, a.day, price                                  
    order by a.day, a.week, price;                                 
quit;

Hopefully this might of some help to you.

Good luck.


Asender...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top