Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: RE: Not sorted error in forvalues loop


From   Dorothy Bridges <dbstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: Not sorted error in forvalues loop
Date   Mon, 5 Oct 2009 12:05:00 -0400

Oh yes, of course.  You are right.  Thank you.

On Mon, Oct 5, 2009 at 12:02 PM, Nick Cox <n.j.cox@durham.ac.uk> wrote:
> Apologies for previous premature post.
>
> My guess:
>
> Second time round the loop, your data start out sorted -id difa1-. But
> you then ask to do things -by id treated:-. Those two aren't consistent.
>
>
> The first line in your loop should perhaps be
>
> bysort id treated : ...
>
> Nick
> n.j.cox@durham.ac.uk
>
> Dorothy Bridges
>
> Stata stops the following loop with a "not sorted" line after the
> "sort id difa`i'" command (not after the subsequent command, after the
> sort command).  What am I missing?  Thanks!!
>
> forvalues i = 1/5 {
> by id treated: gen var_a`i'=.
> replace var_a`i'=ln_var if ante_`i'==1
> by id treated: egen var_a`i'_mean=mean(var_a`i')
> gen difa`i'=.
> replace difa`i'=var_a`i'_mean - var_a`i'_mean[_n-1] if id==id[_n-1]
> replace difa`i'=. if difa`i'==0
> sort id difa`i'
> replace difa`i'=difa`i'[_n-1] if difa`i'==. & id==id[_n-1]
> }
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/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/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index