Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: RE: st: Creating macro varlist for loop - different outcomes with different methods?


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: RE: st: Creating macro varlist for loop - different outcomes with different methods?
Date   Mon, 23 Dec 2013 10:37:48 +0000

<>

wanhaiyou is correct. Felix needs the -of varlist- syntax to insist
that varlists are expanded as such. -in- will not do the job.

For the record, the question has nothing to do with Stata macros. A
varlist is not a macro, although macros may contain varlists.

Nick
njcoxstata@gmail.com


On 23 December 2013 05:39, wanhaiyou <wanhaiyou@hnu.edu.cn> wrote:
> Hi Felix,
> From the help file of "foreach", you can see the following
> example
>
> (1)
> foreach x in mpg weight-turn {
>                 ...
> }
>
>     has two elements, mpg and weight-turn, so the loop will be executed twice.
>
> foreach x of varlist mpg weight-turn {
>                 ...
> }
>
>     has four elements, mpg, weight, length, and turn, because list was given the interpretation of a varlist.
>
> So two syntaxs are different.
>
> Also, the following example may be useful.
>
> foreach q in numlist 1/3 {
>  some commands
> }
>
> foreach q of numlist 1/3 {
>  some commands
> }
>
> There are also different. More detail you can refer the article
> "Speaking Stata: How to face lists with fortitude" by Professor Nicholas J. Cox.
>
>
>> -----原始邮件-----
>> 发件人: "Felix Acker" <facker@vichealth.vic.gov.au>
>> 发送时间: 2013-12-23 13:38:36 (星期一)
>> 收件人: "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
>> 抄送:
>> 主题: RE: st: Creating macro varlist for loop - different outcomes with different methods?
>>
>> Many thanks wanhaiyou, that did the trick.
>>
>> Playing with your code for a bit, I noticed that it is a syntax issue.
>> The following code (yours) works:
>>
>> foreach aa of varlist x1-z {
>> tab `aa', sort
>> }
>>
>> But this code would not work:
>>
>> local MacroListContainer x1-z
>> foreach aa in `MacroListContainer' {
>> tab `aa', sort,
>> }
>>
>> No idea why that is the case, but happy for the solution. Thanks again.
>>
>> Best from Melbourne,
>> Felix
>>
>> Dr Felix Acker
>> Senior Project Officer
>> Knowledge and Environments for Health
>> Victorian Health Promotion Foundation (VicHealth)
>> 15-31 Pelham Street  Carlton  Victoria  3053
>>
>>
>> -----Original Message-----
>> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of wanhaiyou
>> Sent: Monday, 23 December 2013 4:10 PM
>> To: statalist@hsphsun2.harvard.edu
>> Subject: Re: st: Creating macro varlist for loop - different outcomes with different methods?
>>
>> Felix Acker
>> Try:
>>
>> clear
>> set obs 10
>> forv i=1/10 {
>> gen x`i'=uniform()
>> }
>> gen y=uniform()
>> gen z=uniform()
>>
>> foreach aa of varlist x1-z {
>> tab `aa', sort
>> }
>>
>> Bests,
>> wanhaiyou
>> Hunan University
>>
>>
>> > -----原始邮件-----
>> > 发件人: "Felix Acker" <facker@vichealth.vic.gov.au>
>> > 发送时间: 2013-12-23 13:04:16 (星期一)
>> > 收件人: "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
>> > 抄送:
>> > 主题: st: Creating macro varlist for loop - different outcomes with different methods?
>> >
>> > Hello All,
>> >
>> > I have encountered a problem in running a loop on a varlist. My dataset consists of 112 variables, nearly all of them have unique names. I would like to assign them to macros in blocks, for example variable 15 (name = weight) to variable 20 (name = abstinence). I would then like to run a loop on this varlist, using a very basic command such as 'tab' and am expecting separate tables for each variable. When I individually type the variable names for the varlist:
>> >
>> > local TableItems Weight LGA TypeOfWork PosInLocal Aware Abstinence
>> > foreach x in `TableItems' { tab `x', sort }
>> >
>> > This works just fine.
>> > However, when I use the hyphen method to create the varlist, the variables seem to be bound different, in that I get an error message for more than two variables, and a two-way table if my varlist consists of two variables only.
>> >
>> > local TableItems Weight-Abstinence
>> > foreach x in `TableItems' {
>> > tab `x', sort
>> > }
>> >
>> > Would anyone happen to know why this occurs, and how to get around it? In the case of 5 variables, it is easy enough to type them out, but it surely should be possible to do this more efficiently.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index