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: Code-problem - calculating demand elasticities with a QUAIDS


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Code-problem - calculating demand elasticities with a QUAIDS
Date   Wed, 4 Dec 2013 10:20:00 +0000

"afterwards" here is no clue, but it is possible nevertheless to guess
that you are defining

local product 1 2 3 4

in one place (e.g. interactively) and running the rest of the code in
another (e.g. a do-file). That won't work as you intend. Stata can
only see local macros defined in the same place; that is what "local"
means. You are asking -foreach- to cycle over an empty list; that is
legal, but no work is done.

Nick
njcoxstata@gmail.com


On 4 December 2013 10:07,  <franziska.goetze@agroscope.admin.ch> wrote:
> After a few tries now it finally worked.
> "did not work" means: Stata did not show any error code but did not generate w1 w2 w3 and w4.
>
> If I run:
> local product 1 2 3 4
>
> and afterwards:
> foreach k in `product' {
> gen w`k' = x`k' /x14
> replace w`k' = 0 if w`k'==.
> }
> it does not generate w1-w4;
> Only if I run the code altogether, w1-w4 are being generated.
>
> Does this mean I have to run a code that refers to an index, such as "k" here, always together with "local product 1 2 3 4"???
>
>
> -----Ursprüngliche Nachricht-----
> Von: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Nick Cox
> Gesendet: Mittwoch, 4. Dezember 2013 10:38
> An: statalist@hsphsun2.harvard.edu
> Betreff: Re: st: Code-problem - calculating demand elasticities with a QUAIDS
>
> I can't see any problem with that code. I would rewrite
>
> local product 1 2 3 4
> foreach k in `product' {
>        gen w`k' = x`k' /x14
>        replace w`k' = 0 if w`k'==.
> }
>
> as
>
> forval k = 1/4 {
>        gen w`k' = x`k' /x14
>        replace w`k' = 0 if w`k'  == .
> }
>
> but that is just a matter of style. You have to tell us what "did not work" means.
>
> Nick
> njcoxstata@gmail.com
>
>
> On 4 December 2013 09:12,  <franziska.goetze@agroscope.admin.ch> wrote:
>
>> Thanks for your help. The macro for "i" is indeed missing in my do-File, but I haven't figured out how to create such a macro. "i" should have values 1, 2, 3, 4 and so on.
>> I tried it like this:
>>
>> local product 1 2 3 4
>>
>>         and later wanted to refer to "product" (= i = k)
>>
>> foreach k in `product' {
>>
>> gen w`k' = x`k' /x14
>>
>> replace w`k' = 0 if w`k'==.
>>
>> }
>>
>> Unfortunately it did not work. What did I do wrong?
>>
> *
> *   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/

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