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

Re: st: RE: setting global in a loop


From   Ada Ma <pec187@abdn.ac.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: setting global in a loop
Date   Tue, 15 Feb 2005 15:15:40 +0000

Thanks David Harrison and Nick Cox for their answers.

Thanks Nick for his opinion about using the letters in place of numbers - I'm sort of forced into using the char() function because some data files refer to the waves by number and others by letter. I share the data files with a few others so I dare not change their names to fit my purposes.

Cheers!
Ada


Nick Cox wrote:


Taking another look at the code, an alternative is
foreach i in c d e f { global `i'indvars "pid `i'hid" }
The point is mostly one of maintenance.
I wouldn't trust myself to remember that char(99) is "c" for more than a few minutes.
More to the point, on revisiting the code
later a programmer (even the original programmer) who shared this failure of memory would have to work that out afresh. Using the letters directly is more transparent.
Nick n.j.cox@durham.ac.uk


-----Original Message-----
From: Nick Cox Sent: 15 February 2005 12:46
To: 'statalist@hsphsun2.harvard.edu'
Subject: RE: setting global in a loop


This is nothing to do with globals. In fact, the error message is pointing in the right direction.
If you want to use ; as a delimiter you must use it after every (logical) command line. You can't mix EOLs and semi-colons as delimiters
within a block of code. I hardly use this practice, but I surmise that your code should be

#delimit;
foreach i of numlist 3(1)6 { ; local j : di char(`i'+96) ; global `j'indvars "pid `j'hid" ;
}; #delimit cr;

Having said that, there is clearly no compulsion to use ; here.
Incidentally
forval i = 3/6 {
is cleaner and faster than
foreach i of numlist 3(1)6 {
although the "faster" here is a matter of a nanoblink.
Nick n.j.cox@durham.ac.uk
Ada Ma


I have a problem. When I run the following program:

#delimit;
foreach i of numlist 3(1)6 {
local j : di char(`i'+96)
global `j'indvars "pid `j'hid";
}
#delimit cr;

I got an error code r(198) with a comment saying:
"program error: code follows on the same line as open brace"

I am working on the BHPS and I hope that I can draw the same variables out from the various waves using globals combined with a loop. Am I doing anything wrong or is global not allowed in a loop?


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