Announcement

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

  • Matrix by group of observations

    Dear Statalisters,

    I have a panel data of 48 countries, each for 28 years. I want to create 48 matrixes corresponding to each country at once. So each matrix has 28 x 1 dimension. I have tried to use "forvalues", but it does not seem to work. See my codes below:

    forvalues i=1/48{
    mkmat undata if id==`i', matrix(y)
    }

    I am very new to Stata programming.

    Thanks very much for your help.

  • #2
    The reason your code didn't work is that you were creating the matrix y for the first country and then replaced it for each following country. If you replace matrix(y) by matrix(y`i') it should work. Alternatively, use the code snippet below, it will work even if your countries are not nicely defined from 1/48 (and will accept strings even).

    Code:
    levelsof <country>, local(countries)
    foreach <country> of local countries {
         mkmat undata if <country> == `county', matrix(matrix`country')
    }
    replace <country> by the name of your country variable
    Last edited by Jesse Wursten; 11 Jul 2016, 11:53.

    Comment

    Working...
    X