Announcement

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

  • If in foreach

    Good day all,

    I have three files named A B C and four variables named time, timeA, timeB and timeC.

    I regress variables X Y and Z for file A with time variable timeA, X Y and Z for file B with time variable timeB and X Y and Z for file C with time variable timeC

    Stata is indicating that my if command is not correct, that 'A' is not found.

    Does anyone know what I'm doing wrong in this syntax?

    Code:
    foreach period in A B C{
        clear all
        use "C:\`period'.dta"
        replace time=timeA if `period'=="A"
        replace time=timeB if `period'=="B"
        replace time=timeC if `period'=="C"
        foreach var in X Y Z{
            reg diff `var' time growth size
        }
    }

  • #2
    In you current syntax, A is treated as a variable in
    replace time=timeA if `period'=="A" So certainly, variable A is not found. You can change the command to
    if "`period'"=="A" { replace time=timeA } Do the same for the next three commands. Hope it helps.

    Comment


    • #3
      My answer above can only solve your issue with "if". You next looping still seems problematic.

      Comment


      • #4
        The main issue I guess is covered by #6 in http://www.statalist.org/forums/foru...n-a-new-folder See its references.

        Backslashes followed by local macro references won't work as you want.

        You can avoid the backslash any way. Isn't this equivalent?

        Code:
        cd C:
        foreach period in A B C {
            use `period'.dta
            foreach var in X Y Z {
                reg diff `var' time`period' growth size
            }
        }

        Comment


        • #5
          Thanks both!

          @Nick, indeed I will correct the backward slash with a forward slash. I mentioned it incorrectly in my example.

          Gao Liu, the quotes around `period' indeed did the trick.

          Comment


          • #6
            Why do you (think you) need to use slashes or if at all? See my code.

            Comment


            • #7
              Because variable time should be different for each file A B or C that is loaded. At this moment your code excluded this condition. Therefore I needed the correct if-commands, which was resolved by adding quotation marks to the `period' after opening the if-statement.

              Comment


              • #8
                I don't see that. My code automatically uses whichever of timeA timeB timeC fits the chosen period A B C.

                Note the use of the local macro period on the line

                Code:
                 
                 reg diff `var' time`period' growth size

                Comment


                • #9
                  oh : D , I follow. Yes, that is even better. Thanks! Gao Liu's solution is also handy to know, but this works as well and is shorter. Thanks again, both.

                  Comment

                  Working...
                  X