I'm having a little trouble with macro expansion inside a while loop. As a toy example, the following code gives me an error message:

Code:
set obs 10
local i = 1
local xx noisily
while `i' < 5 {
  cap `xx' {
      gen a`i' = runiform() in 1/9
  }
  local i = `i' + 1  
}

Here's the error message I get with Stata 15.1:
Code:
. set obs 10
number of observations (_N) was 0, now 10

. local i = 1

. local xx noisily

. while `i' < 5 {
  2.   cap `xx' {
  3.       gen a`i' = runiform() in 1/9
  4.   }
  5.   local i = `i' + 1  
  6. }
(1 missing value generated)
(1 missing value generated)
(1 missing value generated)
(1 missing value generated)
} is not a valid command name
r(199);
For what it's worth, the same code runs fine without the macro:

Code:
set obs 10
local i = 1
while `i' < 5 {
  cap noisily {
      gen a`i' = runiform() in 1/9
  }
  local i = `i' + 1  
}
Has anyone run into this issue, or something like it, before? If so, what is incorrect with my code? Or is this a Stata bug?

Thanks!