Announcement

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

  • Multiple loop commands into one, "program error: code follows on the same line as open brace”

    Hi everyone,

    I have a panel data set that consists of portfolio weights of 25 different currencies (called "Weight'name of country'") and net return currency spot rates of those 25 countries (called "netreturn_Spot'name of country'") which are both sorted by column. My goal is to compute portfolio returns per country based on a carry investment strategy.

    I tried to “merge” several loop commands to multiply net spot returns of each country by the portfolio weight of the respective country.
    I created the loop command as follows:

    Code:
    foreach var of varlist SpotCzechRepublic - netreturn_SpotUK {
        
        foreach `y' of nettreturn_Spot {CzechRepublic Mexico Austria Belgium Denmark France Germany Italy Netherlands Portugal Spain Iceland Israel Canada Norway Sweden Switzerland UnitedStates Greece Hungary Japan Poland SouthKorea SlovakRepublic UK
            
            foreach `x' of Weight {UK UnitedStates Poland Portugal SlovakRepublic Spain Sweden Switzerland Japan SouthKorea Mexico Netherlands Norway Greece Hungary Iceland Israel Italy France Germany CzechRepublic Denmark Canada Belgium Austria  
                
                gen pfreturn = Weight`y' * netreturn_Spot`x' if `x'==`y' 
    }
    }
    }
    However, I get an error code displaying “program error: code follows on the same line as open brace”.

    Since I am a beginner, I don’t know whether I am on a complete wrong path or I just did a small syntax error. I also noticed in other discussions that you have to define a local first and then run those local variables in one loop, but I failed to apply it in my particular case...

    It would be great if somebody could help me out!

    Thanks,
    Hans

  • #2
    To start, you have one open brace and 3 closed braces. Also, "var" isn't presented after the first line.
    Best regards,

    Marcos

    Comment


    • #3
      I think what you want is the following, where I have shortened the list of countries to make this example code more readable
      Code:
      foreach y of CzechRepublic Mexico Austria Belgium {
          gen pfreturn`y' = Weight`y' * netreturn_Spot`y'
      }

      Comment


      • #4
        The error message tells you exactly what is wrong with your code. After an opining brace, only comments are allowed. In your code, you have to change
        Code:
        foreach var of varlist SpotCzechRepublic - netreturn_SpotUK {
            
            foreach `y' of nettreturn_Spot {CzechRepublic Mexico Austria Belgium Denmark France Germany Italy Netherlands Portugal Spain Iceland Israel Canada Norway Sweden Switzerland UnitedStates Greece Hungary Japan Poland SouthKorea SlovakRepublic UK
                
                foreach `x' of Weight {UK UnitedStates Poland Portugal SlovakRepublic Spain Sweden Switzerland Japan SouthKorea Mexico Netherlands Norway Greece Hungary Iceland Israel Italy France Germany CzechRepublic Denmark Canada Belgium Austria  
                    
                    gen pfreturn = Weight`y' * netreturn_Spot`x' if `x'==`y'
        }
        }
        to something like
        Code:
         gen pfreturn=.
            foreach x in CzechRepublic Mexico Austria Belgium Denmark France Germany Italy Netherlands Portugal Spain Iceland Israel Canada Norway Sweden Switzerland UnitedStates Greece Hungary Japan Poland SouthKorea SlovakRepublic UK {
                          
                    replace pfreturn = Weight`x' * netreturn_Spot`x'
            
        }
        That is at least how I understood your intentions in the previous code. Without having more details about your dataset, I can only make guesses about what might work for your dataset.

        You should also probably read the help file for the foreach-command which you can find via
        Code:
        help foreach
        in Stata. Reading the help file should give you a better understanding of what and why I changed in your code.

        Comment


        • #5
          Thank you guys very much for your help! William Lisowski with your suggestion I could compute monthly portfolio returns per country, that's exactly what I wanted. So, thank you very much again!

          Comment

          Working...
          X