Statalist The Stata Listserver

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

Re: Re: Re: st: re: general varlist in programs

From   n j cox <>
Subject   Re: Re: Re: st: re: general varlist in programs
Date   Tue, 22 May 2007 22:53:36 +0100

This raises the question of the functioning of -nl-,
which did change between 8 and 9, although not
I think in ways that are relevant here. -nl-
pulls off your function name, then passes
the rest through -syntax-, and follows that
by a -tokenize- of the varlist, so it's hard
to see why you should need to do that yourself.

You are doing some tricky things inside your program
with globals and locals within locals. My only
suggestion is to

set trace on

and see where -nl- complains.

The command

global S_1 = "$S_1 B`i'"

is better as

global S_1 "$S_1 B`i'"

to avoid limits on length of string expressions.


Kyle Hood

Just for completeness, here is the function:

program define nlfocsolve
if "`1'" == "?" {
global S_1 = "B0"
global B0 = 1
forvalues i = 1(1)$nx {
global S_1 = "$S_1 B`i'"
global B`i' = 1
replace `1' = 0
local v2 = $nx+2
local v3 = $nx+3
local v4 = $nx+4
forvalues i = 1(1)$nx {
local b`i' = "B`i'"
local v1 = `i'+1
local v5 = $nx + 4 + `i'
replace `1' = `1' + $B0*``v1''*(
``v2''/(invlogit($`b`i'')^2) + ``v3''/((1-invlogit($`b`i''))^2)) +

and here is the function call

. nl focsolve `vl', eps( 1e-12 )

where `vl' is my unabbreviated varlist local macro, which I generated
using my own unabbreviate command. If instead I write

. nl focsolve xd* var1 var0 fd xfreq*, eps( 1e-12 )

then I get the error (described below). Does this make the issue clear?
I'm still not 100% clear on the problem, but I think the issue is that I
am passing a set of arguments to nl that includes the program name
(focsolve) as well as a varlist. This means that it is not a varlist that
is being passed, and able to be automatically unabbreviated.

I have learned a lot from this discussion (I obviously was unaware of
unab, a useful command).


On Tue, 22 May 2007, n j cox wrote:

> We had a thread of eight postings, to which Kit Baum and Maarten Buis
> also contributed.
> We need to see code to comment
> ==============================
> Kyle tried to be kind and shield us from the majority of his
> program, which he guessed was irrelevant to his problem, but he
> was wrong. Whatever his problem is, or was, was, or is, lurking in the code
> he didn't show us. No (visible) code, no (really useful) comment.
> -syntax- and local macro 0
> ==========================
> If you write
> syntax varlist
> what ends up in the local macro varlist is _unabbreviated_. Kit
> demonstrated this, if you needed a demo. I don't know why Michael
> states the contrary. -unab- is useful, but pointing at it
> is a red herring here.
> People usually include -syntax- statements right after -program-
> statements, and that's their natural habitat, but they happily
> function elsewhere. What's key is that -syntax- works on the
> content of the local macro 0. If you invoke a program, or
> a do file, then local macro 0 is automatically defined
> as whatever you typed after the program or do file name
> (that might be nothing, but never mind about that).
> Consider
> -------------------------------
> di "`0'"
> di "`1'"
> di "`2'"
> -------------------------------
> The command
> do whatever I say
> produces the output
> I say
> I
> say
> The first thing displayed is the whole of whatever
> I typed after "whatever", which is in the local macro
> 0. That is then followed by the contents of local macros
> 1 and 2, which are the "words", separated by spaces or
> bound in double quotes, that compose local macro 0.
> Outside a program, you can define local macro 0
> yourself. Inevitably, this will overwrite any previous
> definition. You can type this interactively:
> sysuse auto
> local 0 m*
> syntax varlist
> di "`varlist'"
> and you will see
> make mpg
> The point is that -syntax- looks at local macro 0. We said
> that the contents of that macro were a varlist, and -syntax-
> unwraps it accordingly.
> Local macros 1, 2, etc.
> =======================
> The local macros 1, 2, etc. are, as demonstrated above,
> automatically the first, second, etc., "words" typed
> after the name of a program or a do file. They have
> nothing to do with -syntax-, and running -syntax- doesn't
> change them. As Michael pointed out, you can often usefully
> redefine them in a program using -tokenize-.
> Versions
> ========
> None of this changed between 8 and 9.
> Nick

* For searches and help try:

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