×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• 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.

Calculate conditional MEAN in SAS

 Forum Search FAQs Links MVPs

Calculate conditional MEAN in SAS

(OP)
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!

RE: Calculate conditional MEAN in SAS

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...

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.

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:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!