Announcement

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

  • foreach and local in if statement

    Hey Community,

    I am trying to simplify the following code:

    Code:
    su SA_NewNum if strpos(Scenarios, "Irri_Normal_MoreOwn")
    su SA_NewNum if strpos(Scenarios, "Irri_Extreme_MoreOwn")
    su SA_NewNum if strpos(Scenarios, "Irri_Normal_MoreSell")
    su SA_NewNum if strpos(Scenarios, "Irri_Extreme_MoreSell")
    su SA_NewNum if strpos(Scenarios, "Irri_Normal_MoreNeighbor")
    su SA_NewNum if strpos(Scenarios, "Irri_Extreme_MoreNeighbor")
    su SA_NewNum if strpos(Scenarios, "Irri_Normal_Equal")
    su SA_NewNum if strpos(Scenarios, "Irri_Extreme_Equal")
    su SA_NewNum if strpos(Scenarios, "Irri_Normal_Less")
    su SA_NewNum if strpos(Scenarios, "Irri_Extreme_Less")
    My idea was to create a local for substrings in the variable "Scenario" and then loop:

    Code:
    local actions "Irri_Normal_MoreOwn Irri_Extreme_MoreOwn Irri_Normal_MoreSell Irri_Extreme_MoreSell Irri_Normal_MoreNeighbor Irri_Extreme_MoreNeighbor Irri_Normal_Equal Irri_Extreme_Equal Irri_Normal_Less Irri_Extreme_Less"
    foreach l of local actions {
      su SA_NewNum if strpos(Scenarios, `l')
    }
    Yet, this gives me the following error
    Irri_Normal_MoreOwn not found
    r(111);
    ... which does not make sense to me.

    What am I doing wrong?

  • #2
    The argument is a string, so you need to enclose this within double quotes.

    Code:
    foreach l of local actions {
        su SA_NewNum if strpos(Scenarios, "`l'")
    }

    Comment


    • #3
      One way to think about this is that you expect Scenarios to be interpreted as a variable name. Turn and turn about, Stata expects Irri_Normal_MoreOwn to be a variable (or scalar) name unless you make it clear that it is a literal string. (It's more common that some might guess to want to look for the contents of one variable inside another.)

      Comment


      • #4
        Thank you for your comments!
        @Andew: This unfortunately did not work probably due to what Nick wrote.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          One way to think about this is that you expect Scenarios to be interpreted as a variable name. Turn and turn about, Stata expects Irri_Normal_MoreOwn to be a variable (or scalar) name unless you make it clear that it is a literal string. (It's more common that some might guess to want to look for the contents of one variable inside another.)
          Nick Cox : How would I make clear in the code that Irri_Normal_MoreOwn is a literal string? Andrews solution that clearly refers to it as a string did not produce any results (but also did not show any error)

          Code:
          . foreach l of local actions {
            2.     su SA_NewNum if strpos(Scenarios, "`l'")
            3. }
          
          .
          end of do-file

          Comment


          • #6
            Originally posted by Kerstin Schmidt View Post
            Thank you for your comments!
            @Andew: This unfortunately did not work probably due to what Nick wrote.
            What Nick wrote is a statement of fact. I do not see how that affects the efficacy of my code. We need a data example illustrating your issue.

            Comment


            • #7
              Andrew's solution is exactly right so far as I can tell. I was just adding some further explanation, and not at all contradicting or querying his solution.

              "did not work" is not something we can discuss without an example. Using double quotation marks is what you need, but the quoted strings have to be exactly right.
              Last edited by Nick Cox; 22 May 2024, 04:34.

              Comment


              • #8
                Dear Both,

                Sorry - Andrew's code works perfectly - the problem was on my side.

                Thank you very much!!!

                Comment


                • #9
                  Good to hear that you solved this.

                  Comment

                  Working...
                  X