Hi,

I am having some troubles in computing functions over subgroups.

Basically, I have some financial data over a time period of 4 years for some firms and I would like to create a variable for the mean or the sum of these data by year. You can see in the attachment how my data looks like.

I tried this code:

bysort Date: egen meanPrice= mean(Price),

but it generates the mean for each signle observations, instead of having the mean for each of the years.

I know that with the commad collapse it works, but then I would need to add the new variable with the old dataset and I cannot it merge it.

Could you please help me?

Thank you in advance!