No announcement yet.
  • Filter
  • Time
  • Show
Clear All
new posts

  • Beginner question, loop command for net returns, error code r(198) "`var' invalid name"

    Hi everyone,

    I have a time series set of monthly currency spot rates from multiple countries. I also have portfolio weigths per currency of those countries in the same set (see pic 1), which is supposed to be launched as a carry investment strategy.

    What I wanted to do is to create a loop command in order to compute net spot returns per country. Then I would like create another loop to multiply those monthly net spot returns by the portfolio weights per country.
    I first tested it for the country of Austria, according to the following formula, which worked out fine btw:

    gen time = _n

    gen spotlogreturns2 =(((SpotAustria[_n]) - (SpotAustria[_n-1])) / (SpotAustria[_n]))

    gen PfReturnsAustria = ((spotlogreturns2[_n]) * (WeightAustria[_n]))

    Then I created a loop for the net spot returns based on that formula as follows:

    foreach var of varlist SpotCzechRepublic - SpotUK {
    gen netreturn_`var' = (((`var’[_n]) - (`var’[_n-1])) / (`var’[_n]))

    However, I received error r(198) code dislaying "`var' invalid name"(see pic2). Do you guys have an idea of what went wrong here? SInce, I am a beginner I am not even sure whether I set the loop command right. It would be great if somebody could help me with this!

    Thank you guys so much,
    Attached Files

  • Hans Koslowski

    thank you guys very much for your responses and sorry for posting my question in the wrong forum!
    You guys correctly spotted the error in the wrong formation of the "apostrophe", which indeed occured due to formation in Microsoft Word, where I initially experimented with that loop command.

    So thank you guys again for taking your time and also for providing such a comprehensive explanation!

    Best wishes,

    Leave a comment:

  • Joseph Coveney
    Maybe try something like this
    foreach var of varlist SpotCzechRepublic - SpotUK {
        generate double netreturn_`var' = (`var' - `var'[_n-1] ) / `var'

    Leave a comment:

  • William Lisowski
    Welcome to Statalist.

    You have accidentally posted your topic in Statalist's Mata Forum, which is used for discussions of Stata's Mata language, which is different than Stata's command language, and different than Stata's matrix commands. Your question would have seen a much larger audience if you had posted it in Statalist's General Forum.

    Also, if you have not already done so, take a look at the Statalist FAQ linked to at the top of this page for posting guidelines and suggestions. Screenshots of data and code are frequently less helpful than you might expect.

    However, with all that said, I can point out the problem in your generate command.

    To have Stata substitute the value of a local macro
    you need to type on your keyboard
    where the first character is (on an American English keyboard) the "left single quote" beneath the tilde (~) character below the ESC key, and the final character is the usual "single quote" ("apostrophe") just to the left of the RETURN key. Neither of these are "smart quotes" as understood in programs like Microsoft Word, and in particular the first character is correctly titled the "grave accent".

    Your code contains something like
    in the expression (I can't tell from the picture of your code exactly what it was you typed on the right end), although in the name of the generated variable
    you got it right.
    Last edited by William Lisowski; 17 Feb 2020, 19:34.

    Leave a comment: