Announcement

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

  • Add values for selected years

    Hi. I have a dataset of the form:

    Country year road investment (m) election new_var
    UK 2010 5 No .
    UK 2011 4 Yes 9
    UK 2012 9 No .
    UK 2013 2 No .
    UK 2014 5 No .
    UK 2015 7 Yes 23
    France 2010 6 No .
    France 2011 5 Yes 11
    France 2012 4 No .
    France 2013 6 No .
    France 2014 9 Yes 19

    I want to create a new variablewhich adds up the road investmet between elections for every election year ("new var" in the table is how it should look like). However, I am not sure how to tell stata to only add up the spending since the last election year. I was wondering if someone knows how to do this?

    Thank you very much!















  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str7 country int year byte roadinvestmentm str4 election byte new_var
    "UK"     2010 5 "No"   .
    "UK"     2011 4 "Yes"  9
    "UK"     2012 9 "No"   .
    "UK"     2013 2 "No"   .
    "UK"     2014 5 "No"   .
    "UK"     2015 7 "Yes" 23
    "France" 2010 6 "No"   .
    "France" 2011 5 "Yes" 11
    "France" 2012 4 "No"   .
    "France" 2013 6 "No"   .
    "France" 2014 9 "Yes" 19
    end
    
    by country (year), sort: gen interlude = sum(election[_n-1] == "Yes" | _n == 1)
    by country interlude (year), sort: egen wanted = total(roadinvestmentm)
    replace wanted = . if election == "No"
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      thank you, that works great

      Comment

      Working...
      X