Dear all,
I got the following code because I need to calculate the time-series percentile rankings (a very confusing term for me given that I did not find a very clear definition) for my variable ("var") for each id.
I am kind of unsure if it is correct or not...
To explain in details what I did:
* I calculated the rank of my variable per year (so each id will receive a ranking based on the total number of observations per year (excluding therefore missing values)
* Following that, since I have a panel data from 1995 to 2000, I calculated the average of the rankings that I got for each year (let's say id_3000 had ranking: 1, 10, 15, 18, 19 and 20, the average would be then 13,83)
* After that, each firm has an average ranking for the 6 years and I calculate the median of these average rankings.
* If the average ranking is above the computed median, I would mark the id as a "1" and if not, it is a "0".
Thank you very much if you can help me,
Best regards,
Eugene
I got the following code because I need to calculate the time-series percentile rankings (a very confusing term for me given that I did not find a very clear definition) for my variable ("var") for each id.
I am kind of unsure if it is correct or not...
To explain in details what I did:
* I calculated the rank of my variable per year (so each id will receive a ranking based on the total number of observations per year (excluding therefore missing values)
* Following that, since I have a panel data from 1995 to 2000, I calculated the average of the rankings that I got for each year (let's say id_3000 had ranking: 1, 10, 15, 18, 19 and 20, the average would be then 13,83)
* After that, each firm has an average ranking for the 6 years and I calculate the median of these average rankings.
* If the average ranking is above the computed median, I would mark the id as a "1" and if not, it is a "0".
Code:
sort id bysort year: egen rank = rank(var), track bysort year: egen n = count(var) gen pcrank = ((rank-1)/(n-1)) egen mean = mean(pcrank), by(id) egen median_var = median(mean) gen var_pcrank = 1 if mean > median_var replace var_pcrank = 0 if mean <= median_var replace var_pcrank = . if mean == .
Best regards,
Eugene
Comment