Statalist The Stata Listserver


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

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


From   n j cox <[email protected]>
To   [email protected]
Subject   Re: Re: st: re: general varlist in programs
Date   Tue, 22 May 2007 21:31:03 +0100

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

------------------------------- whatever.do
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
[email protected]

Kyle Hood

This is not the problem. I essentially unabbreviated the list in my loop
before passing it to the program, which worked. I never refer to
"`varlist'", anyway, simply to the variables in the local macros `1', `2',
etc. Also, it's being used for an nl procedure. Maybe I should have
mentioned that. That's probably what the issue is, but it's not a problem
anymore.

Michael Blasnik

> Let me add to this - the error is probably arising somewhere in your program
> when you refer to "`varlist'" and expect it to be fully expanded into a list
> of variables rather than still being in it's varlist state. You may want to
> check out the -unab- command to unabbreviate and expand the varlist and place
> that tokenized list in a local macro.

Michael Blasnik"

>> You've made a typo or some other error -- the syntax for varlist has
>> accepted wildcards as far back as I can recall (Stata for DOS anyone?).

Kyle Hood

>>> I guess they must have improved things from v8 to v9, or there is
>>> something I am doing that is wrong.
>>>
>>> The workaround that I am using is to employ a foreach var of varlist ...
>>> loop, concatenating the variable names into a local variable. Maybe it
>>> would be best for me just use stata 9, though.

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