I know the value that I need to begin with (1994M1) but because this loop is being run in multiple files (which are loaded one by one through another outer loop. The loop in questions is nested inside the outer loop that loads the different datasets).
However, Stata gives me the error "observation numbers out of range", which is understandable because the i++ makes it run the command for observation number _N+1. I've tried various changes to the loop but failed. Any help on how to stop this loop at the last observation would be greatly appreciated. (I've tried encasing the whole thing in forval and while but that results in tbe same input, 1994M1, for every observation).
Code:
gen Year_Month="" order Year_Month local i 1 foreach y of numlist 1994/2020{ foreach m of numlist 1/12{ replace Year_Month = "`y'M`m'" in `i++' } }
0 Response to Restricting Loop to Observation Number
Post a Comment