My data is time series individual data. For some personens in some years there are missing information in time invariant variables. The is an artifact of the data, so it is ok to substitute the missing data with data from previous/following years, i.e., it is ok to assume that the sex of a person is constant over time. My data looks something like this:
Where 'pnr' is the individual id, 'year' denotes the year, and 'male' denotes the sex of the person. For persons where there are information on the time invariant variables in years preceeding the years with missing information (person 2) I can fill out the data with:
However, for the persons (person 3) where there are only data in the years folowing the years with missing information, I have to run the command for every year where there is missing information:
This is tedious to do in my real data, so how do I tell Stata to run the exact same command n times?
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float(pnr year male) 1 1 0 1 2 0 1 3 0 1 4 0 1 5 0 1 6 0 2 1 1 2 2 1 2 3 1 2 4 . 2 5 . 2 6 1 3 1 . 3 2 . 3 3 . 3 4 . 3 5 1 3 6 1 end
Code:
replace male=male[_n-1] if male==. & pnr==pnr[_n-1]
Code:
replace male=male[_n+1] if male==. & pnr==pnr[_n+1] replace male=male[_n+1] if male==. & pnr==pnr[_n+1] replace male=male[_n+1] if male==. & pnr==pnr[_n+1] replace male=male[_n+1] if male==. & pnr==pnr[_n+1]
Comment