Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: while loop problem


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: while loop problem
Date   Fri, 11 Oct 2002 12:28:16 +0100

Chris.Fergusson@sth.nhs.uk
>
> I had a do file set up to calculate the age-specific rates
> for cancer in
> specified health authorities.  However this program became slow and
> laborious as I could only do this for one health authority
> at a time.
> Through website renevation, the person that mainly uses
> this do file is
> running the do file for the same ten health authorities
> every time.  I would
> like to reduce running time by having an option to state
> that you would like
> to look at all ten health authorities, but still have the
> option that you
> may only want to look at a number less than ten.  The
> syntax below runs
> through the while loop one, giving results for the first
> health authority
> "barnsley". However, when it comes to the second run, it
> keeps $ha as
> barnsley, even though my counter q increases in value.  Any
> suggestions, my
> guess is a simple syntax error.

I can't follow all the logic of what you are trying
to do here. But you are doing one thing which
I suspect you do not want to do.

Focusing on your second request, global ha
will contain a name after the user has
given input: let's say it is "barnsley".

You then say, depending on `q',

local $ha "<wherever>"

which Stata interprets as

local barnsley "<wherever>"

That is,

1. Substitution first: the reference to $ha is replaced
by the contents of that macro.

2. Execution second: the resulting statement then
defines a local macro.

That is, what you are doing is indirectly
defining a local macro, which you never
use.

I will guess that asking for a number first is
just the way that you thought of programming
this. However, if I were user I might just want to
type the names all at once, which would mean something like this

display "Which health authorities would you
like to look at? (separate by spaces)" _request(ha)

foreach a of global ha {
	local a = lower("`a'")
	outsheet using `a', replace
 	winexec "C:\program files\microsoft office\office\excel.exe" `a'.out
}

One small detail is that you do not need to oblige
the user to type in lower case.

However, this code -outsheet-s the same data
irrespective of the request. That can't
be right: presumably you need to read in
the data, or specify a subset of the existing
data, first.

Nick
n.j.cox@durham.ac.uk

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index