Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Local macros and strings: yet another query...


From   "Sergiy Radyakin" <[email protected]>
To   [email protected]
Subject   Re: st: Local macros and strings: yet another query...
Date   Tue, 24 Jun 2008 09:32:13 -0400

Hello Daniel,

I am not sure what is going wrong with your code, but may be the
example below will help:

clear
input close volume
  1 10
  2 20
  3 30
end

forvalues t=1/3 {
 local Val1=close[`t']
 local Val2=volume[`t']
 local LoopStr "myCommand,"
 forval j=1/2 {
     local LoopStr "`LoopStr' Opt`j'(\`Val`j'')"
     di `"`LoopStr'"'
 }
}

Here is what it does:
. do z

. clear

. input close volume

         close     volume
  1.   1 10
  2.   2 20
  3.   3 30
  4. end

.
. forvalues t=1/3 {
  2.  local Val1=close[`t']
  3.  local Val2=volume[`t']
  4.  local LoopStr "myCommand,"
  5.  forval j=1/2 {
  6.      local LoopStr "`LoopStr' Opt`j'(\`Val`j'')"
  7.      di `"`LoopStr'"'
  8.  }
  9. }
myCommand, Opt1(1)
myCommand, Opt1(1) Opt2(10)
myCommand, Opt1(2)
myCommand, Opt1(2) Opt2(20)
myCommand, Opt1(3)
myCommand, Opt1(3) Opt2(30)

.
end of do-file

Regards,
   Sergiy Radyakin





On 6/24/08, H�chle, Daniel (MI Switzerland) <[email protected]> wrote:
> Dear all,
>
> Thanks a lot to Sergiy and Nick for their answers. Unfortunately, the project I am currently working on urges me to generate a series of variables on an observation-by-observation basis. The problem is highly iterative. As such the time t-1 value of variable A impacts on the time t value of variable B which in turn determines the time t value of variable A, etc.
>
> The strings I want to evaluate have to be constructed by loops since the number of options varies across "myCommand". Interestingly, however, my "loop strings" do not work with Sergiy's solution whereas for the manually constructed strings his solution works perfectly. Here is an illustration of the problem:
>
> -- snip --
>
>  sysuse sp500, clear
>
>  * This is the manual version of the string:
>  local ManualStr "myCommand, Opt1(\`Val1') Opt2(\`Val2')"
>
>  * Here, the string is replicated by looping through a forval loop:
>  local LoopStr "myCommand,"
>  forval j=1/2 {
>      local LoopStr "`LoopStr' Opt`j'(\`Val`j'')"
>  }
>
>  * Let's assume we want to evaluate "myCommand" for the following values:
>    local t = 122
>    local Val1 = close[`t']
>    local Val2 = volume[`t']
>
> * Display the resulting ManualStr:
>  disp `"`ManualStr'"'   // this is what I would expect
>
> * Display the resulting LoopStr
>  disp `"`LoopStr'"'     // there is a problem: where is `Val1'???
>
> -- snip --
>
> Result: Somehow Stata drops `Val1' from the "LoopStr" whereas `Val1' is contained in the "ManualStr" as it should. Therefore, I wonder whether there is a problem in my way of constructing "LoopStr" or if this is a Stata problem? I use Stata SE 9.2.
>
> Once more thanks a lot for looking into this issue in advance.
>
> Best regards,
> Daniel
>
>
>
>
>
> Any information in this communication is confidential and may be privileged or otherwise protected from disclosure without our consent. If you are not the intended recipient, you are not authorised to and must not disclose, copy, distribute, or retain this message or any part of it. You are requested to please telephone or email the sender and delete this message and any attachment from your system.
>
> Due to the electronic nature of e-mail, there is a risk that the information contained in this message has been modified. Consequently we can accept no responsibility or liability as to the completeness or accuracy of the information.
>
> A-03-03-E
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index