Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: creating/manipulating matrices with nested loop
From 
 
Jin-Hyun Bae <[email protected]> 
To 
 
[email protected] 
Subject 
 
st: creating/manipulating matrices with nested loop 
Date 
 
Wed, 31 Jul 2013 16:39:13 -0400 
Hello!
I am doing my first matrix calculation in Stata and it needs to be
looped for efficiency! I haven't a clue where to start, so I would be
very grateful for any guidance you can provide.
Here is a toy example.
Country     Year    CountryY     Var1      Var2   Var3
ALB          1990    ALB1990     38          40       .
ALB          1991    ALB1991     93          118      .
ALB          1992    ALB1992     88          51       .
DZA          1990    DZA1990     241        15        .
DZA          1991    DZA1991     40          86       .
                            ...........
GBR         1990    GBR1990     67         2         .
GBR         1991    GBR1991     123       33        .
What I would like requires two nested loops.
At the beginning of loop 1, a matrix (A) with varlist "Var1 Var2" is
created for the row with GBR1990.
Then another loop is started, where a similar matrix (B) with varlist
"Var1 Var2" is created for ALB1990.
Then follows a matrix calculation involving A, B, and their covariance
matrix. The resulting scalar is entered as a value of Var3 in the row
with ALB1990.
B is then dropped, and the second loop process repeats, with DZA1990.
After all the Countries with year 1990 are processed, the second loop
stops and A is dropped.
After this, a new matrix A is created for GBR1991 and a similar
process repeats.
In the end, Var3 would have missing values only for the rows with
Country == "GBR".
Jin
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/