I'm working with person-level data grouped by a variable 'year'. Merged into the data is a year-level variable 'tfr.' My goal is to build up text in a local macro containing the value of each year and its corresponding value of 'tfr' (that's a proximate goal, not the ultimate one, but I'm interested in the solution to this problem for its own sake).
I've tried
However, -levelsof- returns a local sorted ascending, not in order of appearance. That means that I can't -sort year- and then run -levelsof tfr- to get the index positions of the two macros to line up.
I can imagine a number of silly ways to achieve what I want (eg -preserve-, -keep if year==`y', push an element into my tfr macro, -restore-). I'm hoping for a non-silly way. Cheers!
I've tried
Code:
levelsof year, local(yrs) levelsof tfr, local (tfrs) local n : word count `yrs' forvalues i= 1/`n' { local y : word `i' of `yrs' local t : word `i' of 'tfrs' local yrtext `yrtext' "`y', `y' <TFR=`t'>" }
However, -levelsof- returns a local sorted ascending, not in order of appearance. That means that I can't -sort year- and then run -levelsof tfr- to get the index positions of the two macros to line up.
I can imagine a number of silly ways to achieve what I want (eg -preserve-, -keep if year==`y', push an element into my tfr macro, -restore-). I'm hoping for a non-silly way. Cheers!
Comment