Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Help with - foreach - loop


From   "Neil Shephard" <[email protected]>
To   [email protected]
Subject   Re: st: Help with - foreach - loop
Date   Wed, 21 Mar 2007 11:54:30 +0000

You probably need to add some quotes, since you are testing equality
of strings and not variables or numbers.

Try...

foreach x of varlist a b c {
if "`x'" == "a" {
  use tempa, clear
          }
if "`x'" != "a" {
  use tempb, clear
          }
qui: do stuff
di "Line 1"
if "`x'" == "a" {
  use tempa, clear
          }
if "`x'" != "a" {
  use tempb, clear
          }
qui: do stuff2
di "Line 2"
}

On 3/20/07, Doogar,  Rajib <[email protected]> wrote:
Dear List,

I am trying to write a 'foreach x of varlist ...' loop in which I want
to use different data sets depending on which variable in varlist is
being processed.  When run, the following program correctly outputs
"Line 1" but fails to load tempa.dta and I cannot figure out why.  Any
tips will be most helpful.  Thanks.

foreach x of varlist a b c {
  if `x'==a {
    use tempa, clear
            }
  if `x'!=a {
    use tempb, clear
            }
  qui: do stuff
  di "Line 1"
  if `x'==a {
    use tempa, clear
            }
  if `x'!=a {
    use tempb, clear
            }
  qui: do stuff2
  di "Line 2"
}

r.

Rajib Doogar,
Department of Accountancy,
The University of Illinois at Urbana-Champaign
1206 S. Sixth Street, Champaign, IL 61820
Ph: 217.244.8083, Fax: 217.244.0902
http://www.cba.uiuc.edu/doogar


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


--
"Every great advance in natural knowledge has involved the absolute
rejection of authority."  - Thomas H. Huxley

Email - [email protected] / [email protected]
Website - http://slack.ser.man.ac.uk/
Photos - http://www.flickr.com/photos/slackline/
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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