Statalist


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

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


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Local macros and strings: yet another query...
Date   Tue, 24 Jun 2008 13:52:55 +0100

Focus on this code and how things pan out as you go round the loop: 

  local LoopStr "myCommand,"
  forval j = 1/2 {
      local LoopStr "`LoopStr' Opt`j'(\`Val`j'')"
  }

local Loopstr is set to "My command,". 

j is set to 1. 

local Loopstr acquires extra text "Opt1(`Val1')" 

local Loopstr thus becomes "myCommand, Opt1(`Val1')"

j is set to 2. 

Now the characters `Val1' are exposed and are evaluated to an empty string. 

Otherwise put, the escape character \ gives one-time protection only. \` means take the following open quote as a literal open quote, not part of a macro reference. But once that's done, the \ disappears. Stata doesn't know that you want it to stay there until its role is done! 

Your code is to demonstrate your problem, but it doesn't convince me that my earlier advice doesn't apply. Re-order the code and all need for a work-around disappears. 

  sysuse sp500, clear

  * Let's assume we want to evaluate "myCommand" for the following values:
    local t = 122
    local Val1 = close[`t']
    local Val2 = volume[`t']
  
  * 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'')"
  }
      
* Display the resulting ManualStr:
  disp `"`ManualStr'"'   

* Display the resulting LoopStr
  disp `"`LoopStr'"'     

Nick
[email protected] 

H�chle, Daniel (MI Switzerland)

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.

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