I am trying to do a permutation test with yearly panel data. The treatment variable, "treat", has 7 categories. For each year, I want to assign one of the seven treatment types to each unit. The total number of units for each year is given as "N[year]" (e.g., N2020), and I want to assign a treatment type to each 1/7 of "N[year]".

Here is my question: is there a way to tell STATA to get the number of types instead of specifying it myself as below (i.e., (N`i')/7)? Here is part of my code:



levelsof year
global yr = "`r(levels)'"
levelsof treat
global tr = "`r(levels)'"

gen treat_rs = .
gen rand = runiform()
sort year rand
gen serial = .

foreach i of global yr {
local counter = 0
foreach k of global tr {
local end = `counter' + (N`i')/7
qui replace treat_rs = `k' if (serial > `counter' & serial <= `end') & (year == `i' )
local counter = `counter' + (N`i')/7
}
}


Thanks so much!