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

RE: st: variable starting point in 'forvalues'


From   "philippe van kerm" <[email protected]>
To   <[email protected]>
Subject   RE: st: variable starting point in 'forvalues'
Date   Mon, 10 Oct 2005 16:31:32 +0200

Dear Gijs,

The solution is probably not where you are looking for it: simply, you
are storing "start" as a local macro (-local start ...- ) and then refer
to it as if it were a global macro (-forvalues i = $start/8- instead of
-forvalues i = `start'/8-). Nothing specific to -forvalues- in fact.

Best wishes

Philippe



>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]
>
>

**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept 
**********************************************************************



*
*   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