error when looping with macros in the indexing -
i trying create dummy variable identify next 5 observations after selection of cutoffs. first method in code below works, looks bit messy , i'd able adjust number of observations i'm creating dummies without typing out same expression 30 times (usually sign i'm doing hard way).
every time put macro indexing, i.e.
[_n-`i']
i following error:
_= invalid name r(198);
i'd grateful advice.
sysuse auto.dta, replace global cutoffs 3299 4424 5104 5788 10371
this works
sort price gen a=0 foreach x in $cutoffs { replace a=1 if price==`x' replace a=1 if price[_n-1]==`x' replace a=1 if price[_n-2]==`x' replace a=1 if price[_n-3]==`x' replace a=1 if price[_n-4]==`x' replace a=1 if price[_n-5]==`x' }
this doesn't.
foreach x in $cutoffs { forval `i' = 0/25 { replace a=1 if price[_n-`i']==`x' } }
any advice why?
in stata terms no loops needed here @ all, except tacit in generate
, replace
. want set counter going each time after hit cutoff, , identify counter values between 1 , 5. here's technique:
sysuse auto.dta, clear global cutoffs 3299,4424,5104,5788,10371 sort price gen counter = 0 if inlist(price, $cutoffs) replace counter = counter[_n-1] + 1 if missing(counter) gen wanted = inrange(counter, 1, 5) list price counter wanted +---------------------------+ | price counter wanted | |---------------------------| 1. | 3,291 . 0 | 2. | 3,299 0 0 | 3. | 3,667 1 1 | 4. | 3,748 2 1 | 5. | 3,798 3 1 | |---------------------------| 6. | 3,799 4 1 | 7. | 3,829 5 1 | 8. | 3,895 6 0 | 9. | 3,955 7 0 | 10. | 3,984 8 0 | |---------------------------| 11. | 3,995 9 0 | 12. | 4,010 10 0 | 13. | 4,060 11 0 | 14. | 4,082 12 0 | 15. | 4,099 13 0 | |---------------------------| 16. | 4,172 14 0 | 17. | 4,181 15 0 | 18. | 4,187 16 0 | 19. | 4,195 17 0 | 20. | 4,296 18 0 | |---------------------------| 21. | 4,389 19 0 | 22. | 4,424 0 0 | 23. | 4,425 1 1 | 24. | 4,453 2 1 | 25. | 4,482 3 1 | |---------------------------| 26. | 4,499 4 1 | 27. | 4,504 5 1 | 28. | 4,516 6 0 | 29. | 4,589 7 0 | 30. | 4,647 8 0 | |---------------------------| 31. | 4,697 9 0 | 32. | 4,723 10 0 | 33. | 4,733 11 0 | 34. | 4,749 12 0 | 35. | 4,816 13 0 | |---------------------------| 36. | 4,890 14 0 | 37. | 4,934 15 0 | 38. | 5,079 16 0 | 39. | 5,104 0 0 | 40. | 5,172 1 1 | |---------------------------| 41. | 5,189 2 1 | 42. | 5,222 3 1 | 43. | 5,379 4 1 | 44. | 5,397 5 1 | 45. | 5,705 6 0 | |---------------------------| 46. | 5,719 7 0 | 47. | 5,788 0 0 | 48. | 5,798 1 1 | 49. | 5,799 2 1 | 50. | 5,886 3 1 | |---------------------------| 51. | 5,899 4 1 | 52. | 6,165 5 1 | 53. | 6,229 6 0 | 54. | 6,295 7 0 | 55. | 6,303 8 0 | |---------------------------| 56. | 6,342 9 0 | 57. | 6,486 10 0 | 58. | 6,850 11 0 | 59. | 7,140 12 0 | 60. | 7,827 13 0 | |---------------------------| 61. | 8,129 14 0 | 62. | 8,814 15 0 | 63. | 9,690 16 0 | 64. | 9,735 17 0 | 65. | 10,371 0 0 | |---------------------------| 66. | 10,372 1 1 | 67. | 11,385 2 1 | 68. | 11,497 3 1 | 69. | 11,995 4 1 | 70. | 12,990 5 1 | |---------------------------| 71. | 13,466 6 0 | 72. | 13,594 7 0 | 73. | 14,500 8 0 | 74. | 15,906 9 0 | +---------------------------+
in fact, text says "the next 5 observations after" code implements not that, cutoff observation too. latter, use inrange(counter, 0, 5)
.
understanding principles explained here crucial question.
for inrange()
, inlist()
see entries and/or this paper.
so, did wrong?
this line
forval `i' = 0/25 {
in illegal unless have defined local macro i
(and rather odd style then). perhaps meant
forval = 0/25 {
although 25 comes from, given problem statement, unclear me. error message isn't helpful, stata struggling make sense of code hole in it, given local macro implied code not defined.
Comments
Post a Comment