Announcement

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

  • Creating Variables for The Year at which They Actually Correspond to

    Hello,

    I am working with a panel dataset that provides information on any monetary gifts that a person received. In addition, there are variables that indicate in what year each gift was received. Thus, in Wave 3, Person A may be recorded having received a $500 gift. However, the gift may have actually been received in Wave 1. Accordingly, I want to create variables that equal the value of the total gift(s) received during the year the gift(s) was received.

    My dataset looks like this:

    Code:
    clear 
    input id time gift1 gift2 g1year g2year 
    111 2010 100 200 2007 2008
    111 2009 50 150 2009 2007
    111 2008 25 125 2008 2007
    111 2007 75 . 2007 .
    222 2010 200 300 2010 2009
    222 2009 75 275 2008 2007
    222 2008 . . . .
    222 2007 . . . .
    end
    However, I want it to look like this:

    Code:
    clear
    input id time gift1 gift2 g1year g2year tgift
    111 2010 100 200 2007 2008 0 
    111 2009 50 150 2009 2007 50
    111 2008 25 125 2008 2007 225
    111 2007 75 . 2007 . 450
    222 2010 200 300 2010 2009 200
    222 2009 75 275 2008 2007 300
    222 2008 . . . . 75
    222 2007 . . . . 275
    end
    As you can see, I want to add up all the gifts recorded as being received in the same year. In the dataset above, you can see that respondent 111 received multiple gifts in 2007. The total of these gifts is $450, which is indicated in the tgift column. Furthermore, you can see that I want missing values to be recorded as zeros. For instance, respondent 111 received no gifts in 2010, so their tgift value is zero.

    Frankly, I am stuck on how to approach this and would sincerely appreciate any guidance.

  • #2
    Code:
    forv yr = 2007/2010 {
        egen g1sum`yr' = sum(cond(g1year==`yr',gift1,.)), by(id)
        egen g2sum`yr' = sum(cond(g2year==`yr',gift2,.)), by(id)
        g gsum`yr' = g1sum`yr' + g2sum`yr'
        capture drop g1sum`yr' g2sum`yr'
    }

    Comment


    • #3
      Thanks so much, George Ford ! That's what I needed. To make the time-varying version, I simply ran the following:

      Code:
      gen gsum =.
      foreach yr = 2007/2010 {
      replace gsum = gsum`yr' if time == `yr'
      }

      Comment


      • #4
        Code:
         
         foreach yr = 2007/2010
        is not legal. In order that people new to Stata or new to loops in Stata are not misled, let's flag that you presumably meant forvalues as in George Ford's answer.

        Comment


        • #5
          Hi Nick Cox, yes, you are correct. Thank you for flagging that!

          Comment


          • #6
            Oops. Thank Nick for the correction.

            Comment

            Working...
            X