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: the use of loop function in Stata


From   Rosie Chen <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: the use of loop function in Stata
Date   Mon, 12 Mar 2012 11:55:44 -0700 (PDT)

Pablo, thank you very much for the advice!



----- Original Message -----
From: Pablo Bonilla <[email protected]>
To: [email protected]
Cc: 
Sent: Monday, March 12, 2012 2:43 PM
Subject: Re: st: the use of loop function in Stata

When you have an error within a loop and you don't where the error is
you should type

set trace on

Run it again and identify where exactly the error is.

On the other hand, it is better to use local instead of global:

local lp "lp1 lp2 lp3 lp4"
display "`lp'"

foreach var of local lp {
    histogram 'var',  by(AI4, col(1))
    sum 'var'
    egen std'var'= sd('var')
    gen sds'var'=0.2*std'var'
    bysort AI4: sum 'var'
    bysort AI4: egen mins'var'=min('var')
    egen min'var'=max(mins'var')
    bysort AI4: egen maxs'var'=max('var')
    egen max'var'=min(maxs'var')
    gen insamp'var'= ('var'= (min'var'- sds'var') & 'var'<= max'var'+ sds'var')
}





local

On Mon, Mar 12, 2012 at 2:29 PM, Rosie Chen <[email protected]> wrote:
> Dear all, this is the first time I use loop function combined with global macro, and I got an error message of ""invalid name" after running the syntax below. Could any of you help identity where the problem is? Thank you very much.
>
>
> ***I have lp1, lp2, lp3, and lp4, and would like to do the same analysis (within the loop) on each of the lp variables.
> global lp "lp*"
> display "$lp"
>
> foreach var of varlist $lp{
>
>     histogram 'var',  by(AI4, col(1))
>     sum 'var'
>     egen std'var'= sd('var')
>     gen sds'var'=0.2*std'var'
>     bysort AI4: sum 'var'
>     bysort AI4: egen mins'var'=min('var')
>     egen min'var'=max(mins'var')
>     bysort AI4: egen maxs'var'=max('var')
>     egen max'var'=min(maxs'var')
>     gen insamp'var'= ('var'>= (min'var'- sds'var') & 'var'<= max'var'+ sds'var')
> }
>
>
> *
> *   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/



-- 
----
best,

Pablo Bonilla

*
*   For searches and help try:
*  http://www.stata.com/help.cgi?searchhttp://www.stata.com/support/statalist/faqhttp://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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index