Announcement

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

  • Create a cumulative variable

    Hi everybody,

    I have a panel data and I need to create a new variable based on a dummy variable which varies over time.

    In particular the new variable should assume a value equal to the sum of 1 that the dummy variable presents up to the considered year.

    I'm trying to obtain a database like the following:

    PRODUCT ORIGIN YEAR VARIABLE X NEW VARIABLE
    A BG 1995 0 0
    A BG 1996 1 1
    A BG 1997 0 1
    A BG 1998 1 2
    A BG 1999 1 3
    B BG 1995 1 1
    B BG 1996 0 1
    B BG 1997 1 2
    B BG 1998 1 3
    B BG 1999 1 4
    A MI 1995 0 0
    A MI 1996 0 0
    A MI 1997 1 1
    A MI 1998 0 1
    A MI 1999 0 1

    Thanks in advance

    Giancarlo

  • #2
    Code:
     
    help sum()

    Comment


    • #3
      Giancarlo:
      expanding on Nick's sound advice, what follows will do the trick:
      Code:
      bysort product  origin : g newvariable= sum(variablex)
      Kind regards,
      Carlo
      (Stata 18.0 SE)

      Comment


      • #4
        Thank you both I have applied:

        xtset pairid Year (pairid is an indicator for each threesome of orig dest and product)

        and then:

        by pairid: g newvari =sum(variablex)

        Thank you very much

        Comment


        • #5
          Giancarlo.
          thanks for closing the thread with this good news.
          Kind regards,
          Carlo
          (Stata 18.0 SE)

          Comment

          Working...
          X