Announcement

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

  • Panel data: need help creating a new variable that is equal to a value at time t=T

    This is a basic question, but I always struggle and forget how to do it.

    I have a panel data of stocks: i=Ticker (tic, string), date is monthly from 2019-present

    One of the variables is market value.

    1- I am trying to create a variable mktvalue_0922: which is the market value on that specific date . That would be a column that takes one value for each ticker. The reason is I want to collapse the data and keep that value.

    Currently the way I am doing it is not clean, here is how:

    ** This gives effectively creates a new mktvalue replacing values on all dates except the one of interest with "."
    gen mktvalue_0922=mktvalue if date==22918

    ** Then I take the max of that, so I can have that coloumn populated by the value on that specific date

    sort tic
    by tic: egen mktvalue_end=max(mktvalue_0922)


    Is there a better way?

    2- I would like to create a variable that is equal to the peak value of that stock in 2021

    Basically I want this, but want to restrict the maximum over the 2021 year, so the one below is incorrect

    sort tic
    by tic: egen mktvalue_max=max(mktvalue) //// How do I specify that it should be taken only over 2021?

    Sorry for the simple not too challenging questions. Hopefully I will remember how to do it going forward.






  • #2
    See https://www.stata-journal.com/articl...article=dm0055 Sections 7 and 9.

    Comment


    • #3
      Thank you. I read it and I ended up using the following:

      *This one create the ending market value in September 2022 (I had to use max, because I did not find something of the sort mktvalueB[date==22918]
      by tic, sort: egen endvalue = max((date==22918) * mktvalueB)


      *This one create the maximum market value over 2021
      by tic, sort: egen maxvalue = max((date<=22645 &date>=22311) * mktvalueB)

      Comment


      • #4
        Code:
        (year(date) == 2021)
        is equivalent and perhaps easier in your second solution.

        Comment

        Working...
        X