Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Counting Efficiently

    Dear all,

    I have a large dataset and want to count observations with particular characteristics. Precisely, I count the number of people in a municipality at that year from a certain nationality. My intuitive solution is to loop over these three dimensions and it works.

    Code:
    forvalues y = 2010(1)2015 {
        foreach k of local gemeinden {
            foreach c of local countries {
                count if origin == `c' & Gemeinde == "`k'" & Jahr == `y'
                replace network1Stock_gem = `r(N)' if origin == `c' & Gemeinde == "`k'" & Jahr == `y'
            }
        }
    }
    However, it is computationally expensive and Stata takes very long to perform the calculations. Therefore, I wanted to ask whether perhaps somebody has an idea to make the calculation more efficient?

    Best,
    Felix
    Last edited by Felix Stips; 22 Jan 2018, 05:23.

  • #2
    Seems to boil down to

    Code:
    bysort Jahr origin Gemeinde: gen wanted = _N

    Comment


    • #3
      lol, true. And it's much faster like that.. Thank you!

      Comment

      Working...
      X