Announcement

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

  • Summing a variable over a set number of observations

    Hi there,
    I am certain this is a stupid question (but I can't figure it out!). I have 2 variables in my dataset: date, recorded monthly, and value.I want to make a new variable (call it var) that =1 if the sum of the values over the past 36 months are positive (and zero otherwise).

    I am struggling with the 36 months part. I cannot figure out how to sum up the values without having something like:
    Code:
    gen yy = year(date)
    gen mm = month(date)
    sort yy mm
    
    gen var = value[_n-1] +value[_n-2] + ... + value[_n-36]
    replace var = 1 if var>0
    replace var =0 if var!=1
    Is there a simpler way to do this?

    Thanks for any help.

  • #2
    Do you want the first 35 months to be missing? Then month 36 would be based on months 1-36, month 37 would be based on months 2-37, 38 would be based on 3 through 38, etc? How many months does the data go?
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    StataNow Version: 19.5 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      Correct. I am going to merge this data later with another file. The first 36 months of the data is not actually important (so missing values are fine). The 37th month will be based on months 1-36. That is, I would like to sum value from [_n-36] to [_n-1] inclusive for every value[_n].

      The data goes from January 1985 to December 2013.
      Last edited by Anthony Voyage; 18 Sep 2014, 23:33.

      Comment


      • #4
        The advice given you in answer to a previous question still applies. The sum you want is a difference between two cumulative sums. See post #9 in

        http://www.statalist.org/forums/foru...hin-a-variable

        Comment


        • #5
          Assuming -date- is a standard Stata date, it is also possible to save a couple of lines by using
          Code:
          sort date
          instead of
          Code:
          gen yy = year(date)
          gen mm = month(date)
          sort yy mm

          For the main problem, the following code will work (based on Nick Cox's previous post).
          Code:
          gen sum = sum(x)
          gen sum_36 = (x-x[_n-36])
          replace sum_36 = x if _n == 36
          gen sum_36_pos = ( sum_36 >0 ) if  sum_36<.


          Comment


          • #6
            Paul: Typos there. You need to use sum once you have calculated it. So references to x after line 1 should all be to sum.

            Comment


            • #7
              Sorry Nick, I forgot about the previous question.
              Thank you all for the help.

              Comment

              Working...
              X