Is there a way to store information in Stata similar to a dictionary in Python or a hash map in other languages?
I am iterating through variable lists that are appended with _1, _2, _3, _4, _5, _6, _7 ... _18 to delineate sections, and I want to sum the number of times the letters "DK" appear in each variable in each section. Right now I have 18 for loops, with each loop iterating through a different section, saving the 'sum' of the total number of DK's in a new variable called DK_1sum, DK_2sum, and then I later produce graphs of that data.
I'm wondering if there is a way to turn all this into a large For loop, and just append the data to a dictionary/array such that the data looks like:
{s1Sum, 25
s2Sum, 56 ...
s18Sum, 101}
Is this possible?
This could be stored in a Stata matrix, a Mata matrix or just ordinary Stata variables.
gen count = .
gen which = _n
qui forval j = 1/18 {
scalar found = 0
foreach v of var *_`j' {
count if strpos(`v', "DK")
scalar found = scalar(found) + r(N)
}
replace count = scalar(found) in `j'
}
list which count in 1/18
For variation, here is a Stata matrix approach.
matrix count = J(18,1,.)
qui forval j = 1/18 {
scalar found = 0
foreach v of var *_`j' {
count if strpos(`v', "DK")
scalar found = scalar(found) + r(N)
}
matrix count[`j', 1] = scalar(found)
}
matrix list count
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments