Dear All, Suppose that I have this data set:
For each `stkcd', I wish to generate a variable `co_base', which is equal to the mean of the first three non-zero and non-missing observations of `co' variable. Any suggestions? Thanks.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input long stkcd float(year co) 5 2000 . 5 2001 0 5 2002 0 5 2003 .10579064 5 2004 .25719556 5 2005 .3125 5 2006 .3125 5 2007 0 5 2008 0 5 2009 0 5 2010 .2011278 5 2011 .3335362 5 2012 .3333333 5 2013 .410831 5 2014 .4705882 5 2015 .4706735 5 2016 .54362416 5 2017 .6111111 5 2018 .5911346 5 2019 .58562183 5 2020 .6002913 5 2021 .6251925 5 2022 .6470588 5 2023 . 13 2000 0 13 2001 0 13 2002 0 13 2003 0 13 2004 . 14 2000 .1853736 14 2001 .012834225 14 2002 0 14 2003 .19567125 14 2004 .4389878 14 2005 .5973589 14 2006 .7038942 14 2007 .0753814 14 2008 .13270143 14 2009 .18029143 14 2010 .1818182 14 2011 .55491906 14 2012 .6562573 14 2013 0 14 2014 0 14 2015 0 14 2016 0 14 2017 .20273237 14 2018 .27272728 14 2019 .27272728 14 2020 0 14 2021 .07711757 14 2022 .2299578 14 2023 .23076923 end
Comment