Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • looping over observations to identify positions

    looping over observations to identify positions

    Today, 00:49
    Dear All,

    I am using stata14. I need to identify observation position. In particular I need to loop over the observations and to retrieve the positions corresponding to the case where variable n3 is not missing. Once I get that position I need to change the value of one variable if a set of condition are satisfied. Here there is a sample of my database, in the database group indicates firms and SH_D_cf4 the stakes of the shareholders by year,

    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(group Year SH_D_cf4 VarSH MeanSH n3)
    1 2014 80 . 80 1
    2 2010 36.1 12.428596 20 .
    2 2010 11.5 12.428596 20 .
    2 2010 11.3 12.428596 20 .
    2 2010 10.3 12.428596 20 .
    2 2010 30.8 12.428596 20 5
    2 2011 36.1 10.73623 25 .
    2 2011 26.9 10.73623 25 .
    2 2011 26.7 10.73623 25 .
    2 2011 10.3 10.73623 25 .
    2 2011 . 10.73623 25 5
    2 2012 36.1 10.73623 25 .
    2 2012 26.9 10.73623 25 .
    2 2012 26.7 10.73623 25 .
    2 2012 10.3 10.73623 25 .
    2 2012 . 10.73623 25 5
    2 2013 36.1 10.73623 25 .
    2 2013 26.9 10.73623 25 .
    2 2013 26.7 10.73623 25 .
    2 2013 10.3 10.73623 25 .
    2 2013 . 10.73623 25 5
    2 2014 36.1 10.73623 25 .
    2 2014 26.9 10.73623 25 .
    2 2014 26.7 10.73623 25 .
    2 2014 10.3 10.73623 25 .
    2 2014 . 10.73623 25 5
    3 2009 12 2.73252 14.666667 .
    3 2009 12 2.73252 14.666667 .
    3 2009 18 2.73252 14.666667 .
    3 2009 . 2.73252 14.666667 .
    3 2009 14 2.73252 14.666667 .
    3 2009 14 2.73252 14.666667 .
    3 2009 18 2.73252 14.666667 7
    3 2010 12 2.73252 14.666667 .
    3 2010 12 2.73252 14.666667 .
    3 2010 18 2.73252 14.666667 .
    3 2010 . 2.73252 14.666667 .
    3 2010 14 2.73252 14.666667 .
    3 2010 14 2.73252 14.666667 .
    3 2010 18 2.73252 14.666667 7

    With the following code I get the positions and VarSH is correctly taken at the selected positions

    gen Flag_Year_stable=0

    su group, meanonly
    forvalues i=1/`r(N)'{
    if n3[`i']!=.{
    local pos=`i'
    display `pos'
    display `pos'+1
    display VarSH[`pos'], VarSH[`pos'+1]
    }
    }

    I need to identify years with a stable ownership structure, So I want to change the value of Flag_Year_stable to one if a set of conditions are satisfied. In particular, for each identified position, I need to

    replace Flag_Year_stable=1 if VarSH[`pos'+1]==VarSH[`pos'] & MeanSH[`pos'+1]==MeanSH[`pos']

    I tried several posiibilities, but I did not get the result. For instance I tried



    by group: gen n2=_n
    by group: egen max_n2=max(n2)

    su group, meanonly
    forvalues i=1/`r(N)'{
    if n3[`i']!=.{
    local pos=`i'
    replace Flag_Year_stable=1 if n2[`pos' ]<max_n2 & VarSH[`pos'+1]==VarSH[`pos'] & MeanSH[`pos'+1]==MeanSH[`pos']
    replace Flag_Year_stable=1 if n2[`pos' ]==max_n2 & VarSH[`pos'-1]==VarSH[`pos'] & MeanSH[`pos'-1]==MeanSH[`pos']
    }
    }

    Any suggestion?

    Thank you very much

  • #2
    I'm not certain, but perhaps this gives you what you want, or at least starts you in the right direction:

    Code:
    bysort group: gen next_varsh=VarSH[_n+1] if !mi(n3)
    bysort group: gen next_meansh=MeanSH[_n+1] if !mi(n3)
    bysort group Year: gen flag=cond( VarSH== next_varsh & MeanSH==next_meansh, 1, 0)
    list, sepby(group)
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Thank you, your suggestion works with an adjustment for the last observation of each group that must be compared with the previous block.

      Comment

      Working...
      X