Hi all,
I am currently trying to isolate the last datapoint in a month.
My dataset looks as follows
date - variable (not stata format) YYYYMMDD
year - variable YYYY
month - variable MM
stock_XYZ - I want the last date from
My dataset is trading data which means the last day of the month is not always the last day available.
My approach was to first isolate the last day of the month and then go from there and get the last datapoint.
The code I used was:
egen date_last = max(date), by(year month)
gen stock_XYZ_last = stock_XYZ if date== date_last
While the approach works it does not give me the right answer.
For example, I get for the first month of my dataset which is not the date 19960131 but rather the date 19960132 which is not possible and does not exist in my dataset.
Generally I get in most cases either one above or one below but not always the right date.
Anyone has a workaround or an idea why this is happening?
Thanks in advance.
Best,
Alex
I am currently trying to isolate the last datapoint in a month.
My dataset looks as follows
date - variable (not stata format) YYYYMMDD
year - variable YYYY
month - variable MM
stock_XYZ - I want the last date from
My dataset is trading data which means the last day of the month is not always the last day available.
My approach was to first isolate the last day of the month and then go from there and get the last datapoint.
The code I used was:
egen date_last = max(date), by(year month)
gen stock_XYZ_last = stock_XYZ if date== date_last
While the approach works it does not give me the right answer.
For example, I get for the first month of my dataset which is not the date 19960131 but rather the date 19960132 which is not possible and does not exist in my dataset.
Generally I get in most cases either one above or one below but not always the right date.
Anyone has a workaround or an idea why this is happening?
Thanks in advance.
Best,
Alex
Comment