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: foreach command


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: foreach command
Date   Tue, 22 May 2012 23:26:59 +0100

That still may be too fast if you have not seen all this before, so
here is it more slowly

display "`: label (foreign) `i''" _n

parses as follows

display               command name
"                        open delimiter for displayed string
`:                        open delimiter for extended macro function call
label (foreign)       look up the value label of the variable foreign
`i'                        for the value held in local macro `i'
'                          close delimiter for extended macro function call
"                         close delimiter for displayed string
_n                       and show a blank line too

Nick

On Tue, May 22, 2012 at 11:15 PM, Nick Cox <njcoxstata@gmail.com> wrote:
> I can't see what you typed in trying my syntax, but I looked again and
> what I suggested was perfectly legal.
>
> Here is an example everyone can try.
>
> sysuse auto
>
> forval i = 0/1 {
>        display "`: label (foreign) `i''" _n
>        regress mpg weight if foreign == `i'
> }
>
> You _must_ type left and right single quotes exactly as I have them:
> the sequence in the -display- command will be ` ` ' ' (left left right
> right).
>
> Nick
>
> On Tue, May 22, 2012 at 11:06 PM, Caliph Omar Moumin
> <sheikmoumin@yahoo.com> wrote:
>> Thank you both.
>> Ronan´s suggestion executed with labeling 1, 2, ....11 in each regression, but i could´t do Nick´s suggestion (it says invalid syntax), I would prefer if it gives the label names of each value which are `Disease names`.
>>
>> I am not an expert in using the stata, i thought the idea of looping was to make more flexible and short.
>>
>> Anyway i guess it is enough to figured out which subchapter each regression belongs.
>> Thank you both again
>>
>> Kind Regards,
>> Caliph Omar Moumin
>>
>> Email:  sheikmoumin@yahoo.com
>>
>>
>>
>> ----- Original Message -----
>> From: Nick Cox <n.j.cox@durham.ac.uk>
>> To: "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
>> Cc:
>> Sent: Tuesday, May 22, 2012 7:19 PM
>> Subject: RE: st: foreach command
>>
>> Ronan's really answered his own question. As you can insert all kinds of stuff inside the loop, the looping command doesn't need an option to decorate each iteration of output. You can just provide your own decoration in exactly the way you want.
>>
>> In fact, there is a second kind of answer, or so I guess. In providing -foreach- and -forvalues- StataCorp were providing fairly standard kinds of looping commands recognisable to programmers with backgrounds in many kinds of languages, and there was no need to make them too quirky or add all kinds of extra syntax as icing on the cake.
>>
>> Nick
>> n.j.cox@durham.ac.uk
>>
>>
>> -----Original Message-----
>> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Ronan Conroy
>> Sent: 22 May 2012 18:00
>> To: statalist@hsphsun2.harvard.edu
>> Subject: Re: st: foreach command
>>
>> On 2012 Beal 22, at 17:13, Caliph Omar Moumin wrote:
>>
>>> I wanted to regress a data with many categories so i used this command
>>>  by subchapter2, sort: xtreg cost duration sex agegroup group, re robust
>>>
>>> the problem is this command does not accept xi: command to change the var 'agegroup' into dummy variable
>>>
>>> then i try to use foreach command, and i appplied it like this
>>>  foreach subchapter2_value in 1 2 3 4 5 6 7 8 9 10 11 {
>>> xi: xtreg cost duration sex i.agegroup group if subchapter2== `subchapter2_value', re robust
>>> }
>>>
>>>
>>> Now the problem is i cannot see the label name in the later case
>>
>> This is a nuisance. The solution I adopt is
>>
>> foreach subchapter2_value in 1 2 3 4 5 6 7 8 9 10 11 {
>> di "*================================================*"
>> di "* Results for Subchapter `subchapter2_value'     *
>> di "*================================================*"
>> di
>> di
>> xi: xtreg cost duration sex i.agegroup group if subchapter2== `subchapter2_value', re robust
>> }
>>
>> This prints an old-fashioned ASCII banner in the output. However, I miss the option to echo the command during execution, as happened with the now-deprecated -for-

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index