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

Comment