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: st: Creating macro varlist for loop - different outcomes with different methods?


From   wanhaiyou <wanhaiyou@hnu.edu.cn>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Creating macro varlist for loop - different outcomes with different methods?
Date   Mon, 23 Dec 2013 13:09:40 +0800 (GMT+08:00)

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.
> 
> 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/


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