Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with calculating functions over subgroups

    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 does not work.

    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!
    Attached Files

  • #2
    Welcome to Statalist.

    You have accidentally posted your topic in Statalist's Mata Forum, which is used for discussions of Stata's Mata language, which is different than Stata's command language. Your question will see a more appropriate, and much larger audience if you post it in Statalist's General Forum.

    Also, if you have not already done so, take a look at the Statalist FAQ linked to at the top of this page for posting guidelines and suggestions, especially sections 9-12 on how to best pose your question. Section 12.1 is particularly pertinent

    12.1 What to say about your commands and your problem

    Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
    ...
    Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.

    Comment


    • #3
      Hi, thanks a lot for the advice!

      Comment


      • #4
        gen year = year(Date)
        egen meanPrice=mean(Price), by(year)

        Comment

        Working...
        X