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

st: variable starting point in 'forvalues'


From   Gijs Dekkers <[email protected]>
To   [email protected]
Subject   st: variable starting point in 'forvalues'
Date   Mon, 10 Oct 2005 14:32:15 +0200

Dear all,

This novice Stata-user is wondering whether or not he can have a 'forvalues'-do loop with a variable starting point.

Consider the following example, which works fine:
******************
example 1
******************

global cntryno1 = 4

if ($cntryno1==2 | $cntryno1==13) {
local waveno 2 3 4 5 6 7 8
}
else {
if ($cntryno1==14) {
local waveno 3 4 5 6 7 8
}
else {
if ($cntryno1==15) {
local waveno 4 5 6 7 8
}
else local waveno 1 2 3 4 5 6 7 8
}
}

foreach i of local waveno {
ALL KINDS OF COMMANDS
}

Basically, this little program applies 'all kinds of commands' for a local macro i, which goes from 1 or 2 or 3 or 4 (depending on cntryno1) up to 8. It seems to work fine, which could be expected, for I more or less copied it from prof. Baum's fine lecture "a little bit of Stata progamming...'.

'Problem solved', you might say 'let's get back to work'. However, curious as I am, and given that the above vectors 'waveno' share most figures, I want to know whether I could also have used a 'forvalues'-do loop.

Consider the second example, which does NOT work.
******************
example 2
******************
* start
if ($cntryno1==2 | $cntryno1==13) {
local start 2
}
else {
if ($cntryno1==14) {
local start 3
}
else {
if ($cntryno1==15) {
local start 4
}
else local start 1
}
}

* the start-macro here does only contain the starting value of the vector 'waveno' and the other integers up to 8 come from the 'forvalues'-do loop.

forvalues i = $start/8 {
use d$cntryno1`i'.dta, clear
ALL KINDS OF COMMANDS
}

The macro 'start' is not accepted, and Stata returns an error code 'invalid syntax'. (How) can I use the macro 'start' in a 'forvalues' do-loop?

Thanks,

Gijs

--
dr. Gijs Dekkers
Federal Planning Bureau
Kunstlaan 47-49
1000 Brussels, Belgium
++32/(0)2/5077413
fax 7373 [email protected], [email protected]



**********************************************************************
Disclaimer: This e-mail may contain confidential information
which is intended only for the use of the recipient(s) named above.
If you have received this communication in error, please
notify the sender immediately and delete this e-mail from
your system.
Please note that e-mail messages cannot be considered as official
information from the Federal Planning Bureau.
**********************************************************************
*
* 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