Bookmark and Share

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]

Re: st: for loops in Mata


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: for loops in Mata
Date   Tue, 29 Oct 2013 12:38:33 +0000

Sounds like

gen nmoves = 0
forval j = 1969/1997 {
      local jm1 = `j' - 1
      replace nmoves = nmoves + (municpality`j' != municpality`jm1') &
inrange(`j', yob_ind - 2, yob_ind + 18)
}

Also -search rowwise, sj- in Stata to get a relevant review paper.


Nick
[email protected]


On 29 October 2013 11:07, Karina Corbett <[email protected]> wrote:
> Dear Statalist members,
>
> I am using Stata 12 for Windows (and Stata 13 for Mac )
>
> I have a dataset with annual updates of the municipality in which people live for all individuals in a birth cohort (n=650 000). The follow-up period spans 30 years, and the variable names are in the format municipality1968, municipality1969, etc. I also have a variable for year of birth. I want is to generate a new variable that indicates how many times a person moved (between municipalities) from two years prior to birth to age 18. Perhaps there is an easier way of doing this (and if there is, I would be happy to know!), but I have tried using Mata interactively, and have run into a problem.
>
> The municipality variables are ordered in the dataset according to year of observation. I started off just checking whether first column differed from second, etc, and added +1 to an indicator variable (cum_move) each time they differed (each time a person moved).  So far so good. However, when I tried to restrict column range according to year of birth, so that for people born in 1970, I would only check columns 1-20, and for those born in 1971, I would check column numbers 2-21, etc, the program just runs seemingly forever (or until I stop it). I transformed year of birth into the variable "yob_ind" that takes on value 1 for person born in 1970, and 2 for person born in 1971, etc.
>
>
> ds municpality*
> local municpality `r(varlist)'
> di"` municpality "
>
> mata
> municpality =st_local("municpality ")
> st_view(K=., ., tokens(municpality), "touse" )
> st_view(cum=., .,("cum_mov"), "touse" )
> st_view(yob=., .,("yob_ind"), "touse")
>
> for (i=1; i<=rows(K); i++) {
>                for (j=yob[i]; j=(yob[i]+19); j++) {
>                               if (K[i,j] != K[i,j+1])                                         {
>                                             cum[i]= cum[i]+1
>                                                                                          }
>                                                                           }
>                                                            }
>
> Does anyone know what I am doing wrong here? Is there a better way of solving this problem?
> I am just starting out using Mata so apologies for poor style or  if I've left out important information.
> Any advice would be greatly appreciated
>
> Karina
>
> --
> Karina Corbett, MPhil
> PhD student, Department of occupational medicine and epidemiology
> National Institute of Occupational Health, Norway
>
>
>
> *
> *   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/

*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index