Announcement

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

  • Generating year variable that is the sum of its previous years

    Hi everyone, would appreciate some help with generating a new variable/ editing my current ones. I am currently working with zip code level data and looking at the number of EV chargers.




    Currently my column for EVChargers only shows the NEW number of chargers for each year, however I wish for the year to show the sum of all chargers from the previous years.
    For instance, for zip code 90002, 2020 has 1 new charger. However, I want the total number of chargers as of 2022, so it should be 2 (since there was 1 in 2016)

    Is there a code that would enable me to do this for all the zip codes and years?

    Thanks

  • #2
    Originally posted by Hans Kasim View Post
    Is there a code that would enable me to do this for all the zip codes and years?
    The function sum() will give you a running sum down observations, so something like the following should work.
    Code:
    bysort zip_code (year): generate int cumulative_charger_tally = sum(EVChargers)

    Comment


    • #3
      Thank you so much!

      Comment

      Working...
      X