I have a data set with 5 variables (example shown below): id, newdate, var1, var2, var3 where var1, var2 and var3 are each 0/1 variables; I want to count the gap in days between the end of a "spell" of 1's for var1 and the start of a "spell" of 1's for var2 given that (1) I am only interested in the var2 spell that is closest to the a var1 spell and that the var2 spell must start on or after the end of a var1 spell (and thus can start with 0); there is a similar question for var2 and var3 except that the var3 spell (of 1's) must start after then end of the var2 spell (of 1's) (and thus cannot be smaller than 1); note that in the example below I have included two "outcome" variables that are the gaps in days (and, yes, I think I want missing when the conditions do not apply); hope that I have inserted the correct counts in gap1_2 and gap2_3 <grin>: note that there may be multiple combinations of such gaps per id; for context, there are about 24,000 id's and 1800 days per id
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int(id newdate) byte(var1 var2 var3) float(gap1_2 gap2_3) 941 19755 0 0 0 . . 941 19756 0 0 0 . . 941 19757 0 0 0 . . 941 19758 0 0 0 . . 941 19759 0 0 0 . . 941 19760 0 0 0 . . 941 19761 0 0 0 . . 941 19762 0 0 0 . . 941 19763 0 0 0 . . 941 19764 0 0 0 . . 941 19765 0 0 0 . . 941 19766 0 0 0 . . 941 19767 0 0 0 . . 941 19768 0 0 0 . . 941 19769 1 0 0 . . 941 19770 0 0 0 . . 941 19771 0 0 0 . . 941 19772 0 0 0 . . 941 19773 0 0 0 . . 941 19774 0 0 0 . . 941 19775 1 0 0 . . 941 19776 1 1 0 0 . 941 19777 0 1 0 . . 941 19778 0 1 0 . . 941 19779 0 1 0 . . 941 19780 0 1 0 . . 941 19781 0 0 0 . . 941 19782 0 0 0 . . 941 19783 1 0 1 . 3 941 19784 0 0 0 . . 941 19785 0 0 0 . . 941 19786 0 0 0 . . 941 19787 0 0 0 . . 941 19788 0 0 0 . . 941 19789 0 0 0 . . 941 19790 0 0 0 . . 941 19791 0 0 0 . . 941 19792 0 0 0 . . 941 19793 1 0 0 . . 941 19794 1 0 0 . . 941 19795 0 0 0 . . 941 19796 0 0 0 . . 941 19797 0 0 0 . . 941 19798 0 0 0 . . 941 19799 0 0 0 . . 941 19800 0 0 0 . . 941 19801 0 0 0 . . 941 19802 0 0 0 . . 941 19803 0 0 0 . . 941 19804 0 0 0 . . 941 19805 0 0 0 . . 941 19806 0 0 0 . . 941 19807 0 0 0 . . 941 19808 0 0 0 . . 941 19809 0 0 0 . . 941 19810 0 0 0 . . 941 19811 0 0 0 . . 941 19812 0 0 0 . . 941 19813 0 0 0 . . 941 19814 0 0 0 . . 941 19815 0 0 0 . . 941 19816 0 0 0 . . 941 19817 0 0 0 . . 941 19818 0 0 0 . . 941 19819 0 0 0 . . 941 19820 0 0 0 . . 941 19821 0 0 0 . . 941 19822 0 0 0 . . 941 19823 0 0 0 . . 941 19824 0 0 0 . . 941 19825 0 0 0 . . 941 19826 0 0 0 . . 941 19827 0 0 0 . . 941 19828 0 0 0 . . 941 19829 0 0 0 . . 941 19830 0 0 0 . . 941 19831 0 0 0 . . 941 19832 0 0 0 . . 941 19833 0 0 0 . . 941 19834 0 0 0 . . 941 19835 0 0 0 . . 941 19836 0 0 0 . . 941 19837 0 0 0 . . 941 19838 0 0 0 . . 941 19839 0 0 0 . . 941 19840 0 0 0 . . 941 19841 0 0 0 . . 941 19842 0 0 0 . . 941 19843 0 0 0 . . 941 19844 0 0 0 . . 941 19845 0 0 0 . . 941 19846 0 0 0 . . 941 19847 0 0 0 . . 941 19848 0 0 0 . . 941 19849 0 0 0 . . 941 19850 0 0 0 . . 941 19851 0 0 0 . . 941 19852 0 0 0 . . 941 19853 0 0 0 . . 941 19854 0 0 0 . . end format %td newdate label values var1 yesno label values var2 yesno label values var3 yesno label def yesno 0 "N", modify label def yesno 1 "Y", modify
Comment