Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Calculate conditional MEAN in SAS

Calculate conditional MEAN in SAS

Calculate conditional MEAN in SAS

Hi all,

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

DATA test;
input Week $ Day Price;
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:

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.


RE: Calculate conditional MEAN in SAS

Hi Zenith.

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


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;                  

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


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;                                 

Hopefully this might of some help to you.

Good luck.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close