Announcement

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

  • generate a new variable (average over every two years)

    Dear StataList-ers!

    Help! I need somebody! My question is very basic.

    Panel data (countries, years). I have a variable, which equals the number of reported victims of human trafficking in each country in year t.
    I am trying to generate a new variable, which would equal the average number of victims in country i for years t and t-1. I mean, "victims" in t-1 an in t = the average of "number of victims" in t-1 and in t.

    I tried
    Code:
     sort country_coded year
    bysort country_coded:gen victims =(Numberofvictimsoftrafficking[_n-1] + Numberofvictimsoftrafficking[_n])/2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long country_coded int(year Numberofvictimsoftrafficking) float victims2
    1 2008 108    .
    1 2009  94  101
    1 2010  97 95.5
    1 2011  84 90.5
    1 2012  92   88
    1 2013  95 93.5
    2 2003  45    .
    2 2004  35   40
    2 2005  37   36
    2 2006  38 37.5
    end
    label values country_coded country_coded
    label def country_coded 1 "Albania", modify
    label def country_coded 2 "Austria", modify
    Obviously, I am wrong. Please, help to sort it out!

    Thanks in advance


  • #2
    if you want the first observation equal to Numberofvictims,
    Code:
    bys country (year): gen victims = cond(_n>1, (Numberofvictims[_n-1] + Numberofvictims[_n]) / 2, Numberofvictims)

    Comment


    • #3
      Another approach

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input long country_coded int(year Numberofvictimsoftrafficking) float victims2
      1 2008 108    .
      1 2009  94  101
      1 2010  97 95.5
      1 2011  84 90.5
      1 2012  92   88
      1 2013  95 93.5
      2 2003  45    .
      2 2004  35   40
      2 2005  37   36
      2 2006  38 37.5
      end
      label values country_coded country_coded
      label def country_coded 1 "Albania", modify
      label def country_coded 2 "Austria", modify
      
      tsset country year
      
      tssmooth ma wanted = Numberofvictims, w(1 1)
      
      list, sepby(country)
      
           +------------------------------------------------+
           | countr~d   year   Number~g   victims2   wanted |
           |------------------------------------------------|
        1. |  Albania   2008        108          .      108 |
        2. |  Albania   2009         94        101      101 |
        3. |  Albania   2010         97       95.5     95.5 |
        4. |  Albania   2011         84       90.5     90.5 |
        5. |  Albania   2012         92         88       88 |
        6. |  Albania   2013         95       93.5     93.5 |
           |------------------------------------------------|
        7. |  Austria   2003         45          .       45 |
        8. |  Austria   2004         35         40       40 |
        9. |  Austria   2005         37         36       36 |
       10. |  Austria   2006         38       37.5     37.5 |
           +------------------------------------------------+

      Comment


      • #4
        I am trying to generate a new variable, which would equal the average number of victims in country i for years t and t-1. I mean, "victims" in t-1 an in t = the average of "number of victims" in t-1 and in t.
        I may have my knees in a twist, but the question seems conceptually problematic. What value do you expect to get for, say, Albania 2009? (108+94)/2 or (94+97)/2? You can't have both in the same variable.

        Comment

        Working...
        X