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

From |
David Kantor <dkantor@jhu.edu> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: RE: expanding a local that contains a list or wildcards |

Date |
Tue, 05 Aug 2003 17:29:21 -0400 |

To Dimitriy Masterov:

You are having the same problem that another Statalist member asked about a few weeks ago.

Your loop refers to `0', `1', `2', `3', etc.

`0' is as you defined it by -unab- (though, prior to that point it would have contained the remainder of the command that invoked myprog -- the part that comes after -myprog-).

`1', `2', `3', etc., are the "words" of the remainder of the command that invoked myprog -- broken into pieces as delimited by spaces (but possibly broken up in other ways if quotation marks are present). They are whatever was typed after -myprog-, which is not necessarily the variables of a varlist.

You can change that, however, by doing

tokenize `0'

just after the -unab-.

Then, your program should work as you expected it to. But there are other, possibly better ways to do it. For example, if you just want to count variables, your loop would be better as...

local k= 0

foreach v of local 0 {

local ++k

}

Another matter to consider is that you program assumes that the only thing to follow the command is a set of variables. If anything else occurs there, the -unab- will fail. You can force the program to accept only commands that contain a varlist:

program def myprog

syntax varlist

Either way, if something other than a variable (or abbreviation thereof) occurs in the command, then the program will fail, but with -syntax-, it fails immediately; without -syntax-, it fails somewhere further down. Also, in addition to forcing the program to accept only -myprog- commands that contain a varlist, that varlist is now placed into the macro varlist. There is no need to do -unab-, and `varlist' takes the place of `0'.

program def myprog

syntax varlist

local k= 0

foreach v of local varlist {

local ++k

}

disp "The number of variables is `k'"

end

And if all you really want is the number of variables, you can shorten this to...

program def myprog

syntax varlist

local k: word count `varlist'

disp "The number of variables is `k'"

end

I hope this helps.

-- David

At 03:44 PM 8/5/2003 -0500, you wrote:

Dear Statalist, Numerous people have suggested using unab rhsvars : `rhsvars'. This is exactly what I am looking for. Thank you very much for your help. I do have a more general question. Let's say I have a silly program that looked like this: ************************************************ program myprog version 8.0 unab 0 : `*' display " `0' " local k=0 while "``k''" !="" { local ++k } local --k display "The number of variables is `k'" end ************************************************* For some reason, even though I redefined the 0 local to contain the expanded variable list (without spaces), the subsequent loop appears to use the original, unexpanded local 0. I am not sure why this is the case. Is it possible to redefine 0? Dimitriy Masterov

David Kantor Institute for Policy Studies Johns Hopkins University dkantor@jhu.edu 410-516-5404 * * 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/

**Follow-Ups**:**RE: st: RE: expanding a local that contains a list or wildcards***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**References**:**st: RE: expanding a local that contains a list or wildcards***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**Re: st: RE: expanding a local that contains a list or wildcards***From:*"Dimitriy V. Masterov" <dvmaster@lily.src.uchicago.edu>

- Prev by Date:
**st: Mahalanobis Distance in Stata?** - Next by Date:
**Re: st: Mahalanobis Distance in Stata?** - Previous by thread:
**Re: st: RE: expanding a local that contains a list or wildcards** - Next by thread:
**RE: st: RE: expanding a local that contains a list or wildcards** - Index(es):

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