Dear all,
I want to create a new variable that has the date of the previous year in it:
This is my code so far:
gen leap = mod(year(FPEDATS), 4) == 0 & (mod(year(FPEDATS), 100) != 0 | mod(year(FPEDATS), 400) == 0)
gen FPEDATSn_1 = FPEDATS - (365 + leap)
format %td FPEDATSn_1
but it doesn't work if the variable FPEDATS is in January and in a leap year as it then substracts one to many (e.g. out of 31jan2016 becomes 30jan2015)
Also if the desired previous year FPEDATSn_1 is a leap year then out of e.g. 31jan2017 becomes 01feb2016
Does someone know how to do it right?
this is part of my data:
Thanks!
I want to create a new variable that has the date of the previous year in it:
This is my code so far:
gen leap = mod(year(FPEDATS), 4) == 0 & (mod(year(FPEDATS), 100) != 0 | mod(year(FPEDATS), 400) == 0)
gen FPEDATSn_1 = FPEDATS - (365 + leap)
format %td FPEDATSn_1
but it doesn't work if the variable FPEDATS is in January and in a leap year as it then substracts one to many (e.g. out of 31jan2016 becomes 30jan2015)
Also if the desired previous year FPEDATSn_1 is a leap year then out of e.g. 31jan2017 becomes 01feb2016
Does someone know how to do it right?
this is part of my data:
Code:
* Example generated by -dataex-. For more info, type help dataex clear input long FPEDATS 20850 22311 20850 22311 22339 22704 22311 22311 22311 22311 22311 22311 22311 22704 22311 22311 22311 22339 22704 12084 13545 15006 16467 16467 12084 13545 16467 17928 19389 22311 15006 13545 15006 16467 17928 19389 20850 22311 12084 13208 15034 15006 22311 15006 16467 17928 19389 20850 12084 13545 15006 16467 17928 19389 13545 16467 17928 19389 22311 13545 18686 12842 13573 15034 15399 15764 16130 16495 16860 17225 17591 17956 18321 18686 19052 19417 19782 20878 21974 22339 22704 11747 12112 12842 13208 13573 14303 14669 15034 15399 15764 16130 16495 16860 17225 17591 17956 18321 18686 19052 end format %td FPEDATS
Thanks!
Comment