I am using Stata 15 for generating my data.
I would like to generate my Outcome y (as continuous variable) and then split them into 5 item-variables.
After that I would like to re-check if sum_item(after split) is equal to y, if not, then Looping should run again until the condition meet (y=sum_item)

The Problem is I will Always get at least 1 case that diff (y-sum_item) is not 0, so it means my Looping command doesn't work in this condition
and I don't know where is the failure on my Code.


Code:
  *Example data

  . list y item1 item2 item3 item4 item5 sum_item in 1/5

     +-------------------------------------------------------+
     |  y   item1   item2   item3   item4   item5   sum_item |
     |-------------------------------------------------------|
  1. | 14       4       2       4       3       1         14 |
  2. | 16       2       1       4       4       5         16 |
  3. | 17       3       4       3       4       3         17 |
  4. | 17       3       1       4       4       5         17 |
  5. | 18       5       4       3       5       1         18 |
     +-------------------------------------------------------+

  *Generate Outcome y
  generate y = round(rnormal(20, 5))
 
  *Generate each item
   forvalues k = 1(1)5 {
    gen item`k' = runiform(1, 5)
    replace item`k' = round(item`k')
    }
    
   egen sum_item=rowtotal(item1 item2 item3 item4 item5)  
   gen diff = y - sum_item
  
  *Looping check if y=sum_item

  forvalues a = 1(1)`=_N'  {
   if run[`a']==`a' {
     forvalues k = 1(1)5 {
      replace sum_item=. if sum_item!=y[_n]
      replace item`k' =. if sum_item==.
      
      replace item`k' = runiform(1, 5) if item`k'==.
      replace item`k' = round(item`k')
      replace sum_item= item1 + item2+item3+item4+item5 if sum_item==.
      replace diff = y - sum_item
      }
     }
    }

*Result after running Looping
. tab diff

       diff |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |        494       98.80       98.80
          6 |          1        0.20       99.00
          8 |          2        0.40       99.40
         10 |          1        0.20       99.60
         12 |          1        0.20       99.80
         14 |          1        0.20      100.00
------------+-----------------------------------
      Total |        500      100.00