I want to loop through a specific subset vector of a string variable in Stata. I have a data set like:
id country effect period
1 US 0.20 2
2 US 0.25 3
3 Japan 0.37 2
4 Germany 0.22 3
5 US 0.11 3
6 Japan 0.43 1
7 Ireland 0.30 1
...
I don't want to loop through all values of the country-variable, but only through specific values, e.g. US and Japan. I tried:
levelsof country if country=="US" | country=="Japan", local(countrylev)
levelsof period, local(periodlev) //periods are 1,2,3,4
mat m = J(2,4,.)
local i=1
local j=1
foreach x of local countrylev {
foreach per of local periodlev {
mat m[`i',`j']=`per' *2
local ++j
mat m[`i',`j']=`per' *3
local ++j
mat m[`i',`j']=`per' *3
local ++j
mat m[`i',`j']=`per' *4
local ++i
local j=1
}
matrix list m
}
However this only loops through "Japan"...