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]

[no subject]



(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" <[email protected]>
> å??é??æ?¶é?´: 2013-12-23 13:38:36 (æ??æ??ä¸?)
> æ?¶ä»¶äºº: "'[email protected]'" <[email protected]>
> æ??é??: 
> 主�: 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: [email protected] [mailto:[email protected]] On Behalf Of wanhaiyou
> Sent: Monday, 23 December 2013 4:10 PM
> To: [email protected]
> 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" <[email protected]>
> > å??é??æ?¶é?´: 2013-12-23 13:04:16 (æ??æ??ä¸?)
> > æ?¶ä»¶äºº: "[email protected]" <[email protected]>
> > æ??é??: 
> > 主�: 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.
> > 
> > Thanks for your help and best from Melbourne, Felix Acker
> > 
> > Dr Felix Acker
> > Senior Project Officer
> > Knowledge and Environments for Health
> > Victorian Health Promotion Foundation (VicHealth)
> > 15-31 Pelham Street  Carlton  Victoria  3053
> 
> 
> *
> *   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/
> 
> IMPORTANT - This email, including all attachments, may be confidential. If you are not the intended recipient, you must not disclose, distribute, copy or use the information contained in this email or attachments. Any confidentiality or privilege is not waived or lost because this email has been sent to you in error. If you have received it in error, please let us know by reply email, or telephone us on (613) 96671333, delete it from your system and destroy any copies. Any representations or opinions expressed in this email are those of the individual sender, and not necessarily those of the Victorian Health Promotion Foundation.
> 
> 
> SPAM DISCLAIMER - VicHealth utilises spam filtering software on our email server. Users acknowledge that while every effort is made to ensure the filtering service operates optimally, it is possible that the SPAM filtering software may inadvertently block an otherwise legitimate email. If you have not received a response to an email youâ??ve sent after 2 days, we recommend you call our office on (61 3) 9667 1333 to confirm receipt.
> 
> 
> *
> *   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/


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