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]

st: RE: Interruption of a loop because of missing variables


From   "Cohen, Elan" <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: Interruption of a loop because of missing variables
Date   Fri, 19 Nov 2010 15:12:42 -0500

Hi Florian.  Try this:

foreach file in acer.dta adobe.dta affiliated.dta ... {

  use "`file'"

  cap keep id cit_*
  if _rc==0 {
    reshape long cit_, i(id)
    drop if cit_ == ""
    drop _j
    ren cit_ pat_no

    save "sic_`file'", replace
  }
}

- Elan


> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of Florian Seliger
> Sent: Friday, November 19, 2010 14:26
> To: [email protected]
> Cc: [email protected]
> Subject: st: Interruption of a loop because of missing variables
> 
> Dear Statalist,
> 
> I want to run a foreach loop for 250 files. There are some files were
> variables are missing so that the loop is interrupted.
> 
> I have already tried to use the capture command, but I haven't found an
> adequate specification yet.
> 
> Please see the following example:
> 
> foreach file in acer.dta adobe.dta affiliated.dta ... {
> 
> use "`file'"
> 
> keep id cit_*
> reshape long cit_, i(id)
> drop if cit_ == ""
> drop _j
> ren cit_ pat_no
> 
> save "sic_`file'", replace
> }
> 
> ***end of the example***
> 
> Generally, I have multiple cit_* variables in each dataset. The number
> is varying among the files. For some datasets, I may have 100 cit_*
> variables (cit_1, cit_2, ..., cit_100), but for others zero.
> 
> How can I tell STATA that the commands should not be executed when
> there are zero cit_* variables and that I want STATA to proceed with
> the next file in this case?
> 
> Thank you very much,
> Florian
> 
> 
> 
> 
> 
> --
> GMX DSL Doppel-Flat ab 19,99 &euro;/mtl.! Jetzt auch mit
> gratis Notebook-Flat! http://portal.gmx.net/de/go/dsl
> *
> *   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index