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: loop question


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: loop question
Date   Tue, 17 Dec 2013 10:27:26 +0000

Basic technique is to _think_ your way through a loop at least twice.
Here the first time round -k- is set to 1 and so the first -save- is
skipped.

It is not clear that you need to work with -k- at all. The code seems
to reduce to

forval i = 1/37 {
    forval j =2/5 {
        import excel `i'_`j'_dieta, sheet("Sheet1") firstrow clear
        save `i'_`j'_dieta, replace
    }
}

Note that the -foreach- loop can be simplified to another -forval- loop.
Nick
njcoxstata@gmail.com


On 17 December 2013 10:15, arosella <arosella@irccsdebellis.it> wrote:
> Dear Statalisters,
> this is sure a trivial question but the loop below doesn't save the file
> 1_2_dieta
>
>
>  local k = 1
> forval i = 1/37 {
>     foreach j of numlist 2 3 4 5 {
>         if `k' == 1 {
>             import excel `i'_`j'_dieta, sheet("Sheet1") firstrow clear
>         }
>         else {
>             save `i'_`j'_dieta, replace
>         }
>         local k = `k' + 1
>     }
> }
> Can someone help me?
> Thanks
>
> Alberto R. Osella
> Laboratorio di Epidemiologia e Biostatistica
> IRCCS Saverio de Bellis
> Via Turi, 27
> 70013 Castellana Grotte (BA)
> Italia
> tel: +39 0804994655
> fax: +39 0804994650
> e-mail: arosella@irccsdebellis.it
>
> *
> *   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