Announcement

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

  • Replacing _N by the number of months with non-missing data

    Originally posted by Richard Williams View Post
    I may be getting too cutesy here, but this might be able to take the place of your current code, since you just want the year-end numbers:

    Code:
    gen year = year(Date)
    bysort idcode year: egen sumchg = total(log(chg/100 + 1))
    bysort idcode year: gen annual = exp(sumchg) ^ (12/_N) - 1
    This assumes there is no missing data. If there is, _N needs to be replaced by the number of months with non-missing data. Which can be done, but I won't bother if MD is not an issue.
    Missing data is an issue for my dataset. I replaced _N with the number of months with non-missing data but I made a little mistake

    In the following example I have an individual identifier (i) and a time variable (t). I want to create var4 which counts the number of non
    missing observations of x by I and t, but I want it to start counting when a missing value appears on x.

    Code:
    sort i t
    gen var4=0
    replace var4=1 if i>i[_n-1] & x<.
    replace var4=1 if i==i[_n-1] & x[_n-1]==.
    replace var4=1 if _n==1
    replace var4=0 if x==.
    replace var4=var4[_n-1]+1 if i==i[_n-1] & x<.
    recode var4 (0=.)
    Code:
     i t x var4
    ------------------
    1 1 1 1
    1 2 0 2
    1 3 1 3
    1 4 . .
    1 5 1 1
    ------------------
    2 1 1 1
    2 2 1 2
    2 3 . .
    2 4 0 1
    2 5 0 2
    I want to switch _N in the above formula of Richard with 4 in the case where (i=1) has 1 missing value and with 4 in the in the case where (i=2) has 1 missing value.

    However, my var4 doesn't start counting from 1 after (i) changes
    Last edited by LydiaSmit; 13 Aug 2014, 01:03.

  • #2
    Forget the above post.

    I've found an easier way.
    Last edited by LydiaSmit; 13 Aug 2014, 01:19.

    Comment


    • #3
      Code:
      clear
      input i t x
      1 1 1
      1 2 0
      1 3 1
      1 4 .
      1 5 1
      2 1 1
      2 2 2
      2 3 .
      2 4 1
      2 5 2
      end
      
      sort i t
      by i : egen N = total( !missing(x) )
      
      list, sepby(i)
      The key line in this example is by i : egen N = total( !missing(x) ). You can read that line as follows:
      • missing(x) returns a 1 when x is missing and a 0 otherwise
      • ! is a negation operator, so it turns 0s into 1 and 1s into 0s. So in my mind I always read !missing(x) as "not missing x", which returns a true (1) when x is observed and a false (0) when x is missing.
      • The egen function total() just adds up whatever is within the parentheses, in this case 1s when x is observed and 0s when x is missing. So it counts the number of nonmissing occurences of x
      • by i : means that we count the number of nonmissing occurances of x within each individual.
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Code:
         
        x < .
        is (in this case and others) an alternative to

        Code:
         
        !missing(x)
        There are several objections to it, not least that it hinges upon an arbitrary decision in Stata to regard numeric missings as very large, but brevity is not among them.


        Comment


        • #5
          Forget the above post. I've found an easier way.
          This treats the forum as a way to get answers to an individual's problems in public. You are implying that your problem should be of interest to others; if so, any solution you found should be of interest too and explaining solutions is a way to give something back.

          Comment


          • #6
            Thank you for the help.

            Comment

            Working...
            X