Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: FW: nested local


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: FW: nested local
Date   Fri, 1 Apr 2011 14:43:37 +0100

des x*, varlist

foreach var of varlist `r(varlist)' {

can easily be contracted to

foreach var of varlist x* {

as the signal that what follows is a varlist is entirely enough to
signal to -foreach- that
x* should be expanded.

Other ways of seeing this:

1. The help for -foreach- has examples with wildcards.

2. If the argument supplied to

foreach ... of varlist ....

could not be expanded on the fly to a list with individual names all
explicit, then there would be less point to the syntax. (Not zero
point, as there is a checking that what you claim to be a varlist is
indeed one such.)

Nick

On Fri, Apr 1, 2011 at 2:13 PM, Oliver Jones
<ojones@wiwi.uni-bielefeld.de> wrote:

> Or maybe something more easy using describe to get your varlist:
>
> clear
> set obs 10
>
> * create 5 variables
> forval i = 1/5 {
>        gen x`i' = `i'
> }
>
> * create the varlist
> des x*, varlist
>
> foreach var of varlist `r(varlist)' {
>        replace `var' = 9
> }
> list, tab
>
>
> Best
> Oliver
>
> Am 01.04.2011 14:36, schrieb Francesco Fasani:
>>
>> Hi,
>>
>> My question is the following:
>>
>> I´m trying to run a loop with a local that contains a local and I don´t
>> manage to make it
>> work. I wrote a simplified version below. Basically, it stops at summarize
>> where instaed of
>> summarizeng the variable TR_democr_y_y1 (as I would like it to do) it
>> stops and says that
>> "TR_democr_y_y is an ambiguous abbreviation (because there are five
>> variable called in that
>> way in my dataset: TR_democr_y_y1 ... TR_democr_y_y5.
>>
>> local variables TR_democr_y_y`j'
>>
>> foreach var of local variables {
>>
>> forvalues j=1(1)5 {
>>
>> display `j'
>>
>> di "TR_democr_y_y`j'"
>>
>> su TR_democr_y_y`j'
>>
>> su `var'
>>
>> }
>>
>> }
>>
>> I know I coudl just rewrite the loop as I did below and it woudl work. But
>> the loop I am
>> trying to run is fairly more complex and cannot be written in the version
>> below.
>>
>> local variables TR_democr_y_y
>>
>> foreach var of local variables {
>>
>> forvalues j=1(1)5 {
>>
>> display `j'
>>
>> di "TR_democr_y_y`j'"
>>
>> su TR_democr_y_y`j'
>>
>> su `var'`j'
>>
>> }
>>
>> }
>>

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


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index