Announcement

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

  • Creating Delta Variables

    Hello everyone,

    I have got a time variable (uinique column) which has years (2000,2003,2006,2009,2011) and different rates (different columns)

    How can create a delta variable with only the previous year? The thing I need is to create cross section variables from this panel I already have.

    So, for example, if i want to create the delta between employment in 2006 minus employment in 2003, the variable I would like to create Its something like deltaemployment0603.


    There are a lot of variables and a lot of years (also different places for these rates and years) so manually it would be really difficult.


    Hope you guys can help me out, Thanks!

  • #2
    Dear 090:

    Your data structure isn't clear to me. If you have a so-called wide structure with variables like

    foo2000 foo2003 ... foo2011

    you need a loop like this

    Code:
     
    tokenize 2000 2003 2006 2009 2011 
    
    forval j = 2/5 { 
          local J = `j' - 1
          gen deltafoo``j''``J'' = foo``j''  - foo``J''
    }
    If you have a long structure; just use by: and subscripts.


    Please note the still unsatisfied request within http://www.statalist.org/forums/foru...riod-variables

    Comment


    • #3
      If I'm reading it right, it's actually in long format? If you could post a snippet of data (say 20 obs or so) of what you *have* and another of what you *want* we'd have a better chance at helping. Nick's syntax looks good to me for wide format, but I think you have long. Also, variable names (for example, are there regularities in the names) matter. So unless Nick read your mind just right, please post some data.

      Comment


      • #4
        BTW, if it's long, this syntax
        Code:
        sort id year
        tokenize 2000 2003 2006 2009 2011 
        foreach var of varlist x1-x10 {
                forvalues j=2/5 {
                    local J=`j'-1
                    gen delta`var'``j''_``J''=`var'-`var'[_n-1] if year==``j''
                    }
        }
        should do the job, though Nick probably has a shorter, cleaner way.

        Comment

        Working...
        X