Hello,
I am trying to generate new variable, let's call it "current_year_loyalty", which records the number of years in a row a donor has made at least one donation. So for the first year in which they donate, current_year_loyalty would be assigned a value of 0. If they also made a donation the following year, current_year_loyalty would be assigned a value of 1. However, if they stop donating the year after that, and then resume in some later year, the current_year_loyalty would be reset back to 0.
So in the above example, the user identified by Pidm 70004546 would be assigned the following values:
I am trying to generate new variable, let's call it "current_year_loyalty", which records the number of years in a row a donor has made at least one donation. So for the first year in which they donate, current_year_loyalty would be assigned a value of 0. If they also made a donation the following year, current_year_loyalty would be assigned a value of 1. However, if they stop donating the year after that, and then resume in some later year, the current_year_loyalty would be reset back to 0.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input long PPidm double FiscalYearGifts float FISCALYEAR 70004546 88 2007 70004546 45 2010 70004546 45 2011 70004546 45 2012 70004546 45 2013 70004546 45 2014 70004546 75 2017 70004546 175 2018 end format %ty FISCALYEAR
- 2007: current_year_loyalty = 0
- 2010: current_year_loyalty = 0
- 2011: current_year_loyalty = 1
- 2012: current_year_loyalty = 2
- 2013: current_year_loyalty = 3
- 2014: current_year_loyalty = 4
- 2017: current_year_loyalty = 0 (reset to 0 here because they made no donations in 2015)
- 2018: current_year_loyalty = 1
Comment