Hi, I have a group variable and for each group, I have a string variable of interest (nasdaq). For each group, I would like to identify the last nonmissing value of the string variable and then use it to replace that string variable.
If it weren't for the group variable, I can do:
gen obs=_n
summarize obs if !missing(nasdaq), meanonly
replace nasdaq=nasdaq[r(max)]
Now if I do:
by group: gen obs=_n
by group: summarize obs if !missing(nasdaq), meanonly
by group: replace nasdaq=nasdaq[r(max)]
obviously, the last step fails (since Stata uses the last group's r(max) to repeat the last line for all groups).
My question is, is there a way of combining the last two lines, say
by group: {summarize obs if !missing(nasdaq), meanonly
replace nasdaq=nasdaq[r(max)]}
?
I'd be most grateful for all your help!
Best,
John
If it weren't for the group variable, I can do:
gen obs=_n
summarize obs if !missing(nasdaq), meanonly
replace nasdaq=nasdaq[r(max)]
Now if I do:
by group: gen obs=_n
by group: summarize obs if !missing(nasdaq), meanonly
by group: replace nasdaq=nasdaq[r(max)]
obviously, the last step fails (since Stata uses the last group's r(max) to repeat the last line for all groups).
My question is, is there a way of combining the last two lines, say
by group: {summarize obs if !missing(nasdaq), meanonly
replace nasdaq=nasdaq[r(max)]}
?
I'd be most grateful for all your help!
Best,
John
Comment