Announcement

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

  • use local in foreach command

    Dear All, I ran the following command
    Code:
    local in1 "inrange(year,2011,2012)"
    local in2 "inrange(year,2009,2014)"
    
    foreach v of local in1 in2 {
        reghdfe lPAY3 Treat Post Treat*Post if `v', a(stkcd year) vce(cl stkcd)           
    }
    but found the error message as
    Click image for larger version

Name:	local.png
Views:	1
Size:	6.8 KB
ID:	1614505

    Does anyone know what's wrong with the code? Thanks.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    The help file shows

    Code:
    foreach lname of local lmacname
    meaning that foreach expects exactly one local macro name. You want

    Code:
    foreach inrange in in1 in2 {
        ... if ``inrange''
    }
    or

    Code:
    foreach inrange in "`in1'" "`in2'" {
        ... if `inrange'
    }

    Comment


    • #3
      Code:
      local in1 "inrange(year,2011,2012)"
      local in2 "inrange(year,2009,2014)"
      
      foreach v in `in1' `in2' {
          reghdfe lPAY3 Treat Post Treat*Post if `v', a(stkcd year) vce(cl stkcd)           
      }
      Best regards.

      Raymond Zhang
      Stata 17.0,MP

      Comment


      • #4
        Originally posted by Raymond Zhang View Post
        Code:
        local in1 "inrange(year,2011,2012)"
        local in2 "inrange(year,2009,2014)"
        
        foreach v in `in1' `in2' {
        reghdfe lPAY3 Treat Post Treat*Post if `v', a(stkcd year) vce(cl stkcd)
        }

        Raymond has essentially the same idea as #2. Note, however, that this code will fail if River includes spaces in the inrange() statements, which is legal syntax (and recommended by many style-guides):

        Code:
        . local in1 "inrange(year, 2011, 2012)"
        
        . 
        . foreach inrange in `in1'  {
          2.     display `"`inrange'"'
          3. }
        inrange(year,
        2011,
        2012)

        Comment


        • #5
          Another way,
          Code:
          local in inrange(year,1935,1940) inrange(year,1941,1954)
          foreach v of local in  {
              reghdfe invest mv ks  if `v', a(company year)           
          }
          Best regards.

          Raymond Zhang
          Stata 17.0,MP

          Comment


          • #6
            Originally posted by Raymond Zhang View Post
            Another way,
            Code:
            local in inrange(year,1935,1940) inrange(year,1941,1954)
            foreach v of local in {
            reghdfe invest mv ks if `v', a(company year)
            }
            Same caveat: beware of spaces.

            Comment


            • #7
              Dear daniel, Thanks, and it works.
              Ho-Chuan (River) Huang
              Stata 19.0, MP(4)

              Comment


              • #8
                Dear Raymond, It works as well.
                Ho-Chuan (River) Huang
                Stata 19.0, MP(4)

                Comment


                • #9
                  Code:
                  local in1 inrange(year, 2011, 2012)
                  local in2 inrange(year, 2021, 2022)
                  
                  local inx \`in1' \`in2'
                  
                  foreach inrange of local inx {
                  
                      display "`inrange'"
                  }

                  Comment


                  • #10
                    Dear Bjarte, Thanks for this additional, helpful suggestion. It works well.
                    Ho-Chuan (River) Huang
                    Stata 19.0, MP(4)

                    Comment

                    Working...
                    X