Hi all,
I am running the following code:
This is a small example of my data:
My problem is that the loop only considers the first observation when replacing the difftime1, difftime2 etc. variables. For example, in the second line of the data, the variable difftime2 should contain the value 161 instead of 0. I think the reason for this is that Stata only considers the value of difftime from the first observation, which is equal to one. Therefore the loop always goes to the else-condition after the first round.
When I sort the data in descending order after difftime, the loop never goes to the else-condition and replaces the variables until the maximum of 8730 is reached.
Hope I have described the problem clearly.
Thanks in advance
Philip
I am running the following code:
Code:
local timediff = difftime forvalues i=1/8730{ qui gen difftime`i' =. if `timediff' > 0 { qui replace difftime`i' = timeid + `timediff' } else { qui replace difftime`i' = 0 } local timediff = `timediff'-1 }
This is a small example of my data:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(difftime timeid difftime1 difftime2 difftime3) 1 158 159 0 0 5 159 160 0 0 2 160 161 0 0 2 160 161 0 0 3 162 163 0 0 6 296 297 0 0 9 302 303 0 0 9 302 303 0 0 19 352 353 0 0 19 352 353 0 0 6 684 685 0 0 38 817 818 0 0 726 860 861 0 0 7 870 871 0 0 7 870 871 0 0 40 984 985 0 0 3 1586 1587 0 0 223 1591 1592 0 0 3 1784 1785 0 0 27 1814 1815 0 0 90 1841 1842 0 0 313 1846 1847 0 0 1 2610 2611 0 0 1 2610 2611 0 0 33 2612 2613 0 0 10 2645 2646 0 0 10 2645 2646 0 0 2 2795 2796 0 0 2 2795 2796 0 0 58 2925 2926 0 0 58 2925 2926 0 0 4 2983 2984 0 0 4 2983 2984 0 0 4 3588 3589 0 0 4 3588 3589 0 0 2 3608 3609 0 0 3 3608 3609 0 0 1 3608 3609 0 0 11 3619 3620 0 0 41 3671 3672 0 0 41 3671 3672 0 0 28 3924 3925 0 0 28 3924 3925 0 0 end
When I sort the data in descending order after difftime, the loop never goes to the else-condition and replaces the variables until the maximum of 8730 is reached.
Hope I have described the problem clearly.
Thanks in advance
Philip
Comment