BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
iank131
Quartz | Level 8

I am trying to calculate the daily trade-weighted average of stock prices. That is, I want one daily price per stock which is a calculated as the weighted average price based on the traded volume on each day. I have many stocks and many trades each day per stock. I am able to do this using a combination of data steps and proc means, and even with proc SQL (from ideas seen in another post), but is there are more elegant way of doing it? :smileyconfused:

I tried PROC EXPAND, but since I have a different number of obs in each BY group, I am not sure it is possible. It would be great if it could :smileycool:. For example, my data looks something like the following, and my BY group would be CUSIP DATE, my analysis variable would be PRICE, and my WEIGHTS are trdsize:

Stock (CUSIP)DatePriceWeights (trdsize)
A110100
A111200
A112300
A111350
B130300
B132400
B234100

What I want is this:

CUSIPDatetrade_wght_price
A110*100/(100+200+300+350) + 11*200/950 + 12*300/950 + 11*350/950
B130*300/(300+400) + 32*400/700
B234*100/100

I attach a data file with a more comprehensive sample.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

I am a little surprise . You can't get it by proc means ?

data have;

input stock $ date price weights;

cards;

A 1 10 100

A 1 11 200

A 1 12 300

A 1 11 350

B 1 30 300

B 1 32 400

B 2 34 100

;

run;

proc summary data=have;

by stock date;

var price;

weight weights;

output out=want(drop=_:) mean=weight_price;

run;

Xia Keshan

View solution in original post

2 REPLIES 2
Ksharp
Super User

I am a little surprise . You can't get it by proc means ?

data have;

input stock $ date price weights;

cards;

A 1 10 100

A 1 11 200

A 1 12 300

A 1 11 350

B 1 30 300

B 1 32 400

B 2 34 100

;

run;

proc summary data=have;

by stock date;

var price;

weight weights;

output out=want(drop=_:) mean=weight_price;

run;

Xia Keshan

iank131
Quartz | Level 8

Thanks very much. I learned something about PROC SUMMARY.

As I said I tried it with proc means, but I didn't really understand the examples that were given using the weight option. You helped me clarify it, and elegantly too Smiley HappySmiley Happy

Ian.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 11357 views
  • 2 likes
  • 2 in conversation