Announcement

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

  • Issues with local macro

    Hello,

    I'm running a diff-in-diff using a policy with three phases of treatment. There's the issue of staggered treatment, but I still want to see the results of the separate phases.

    Suppose I have two cohorts 0 and 1 which I use as the "before-after" of the diff-in-diff. The states that were treated in phases 1, 2 and 3 were (A, B, C), (D, E, F) and (G, H, I) respectively. I want to run a diff-in-diff for each phase by dropping the treated states in the other phases. E.g. for phase 1, I drop (D, E, F) and (G, H, I) and compare (A, B, C) to the control/remaining states. Since I'll have to do this for each phase and a bunch of outcome variables, I thought using local macros and running a loop over them would be useful.

    My code looks like this:

    use Data, clear

    local all_trt_states " "A" "B" "C" " " "D" "E" "F" " " "G" "H" "I" "

    foreach s of local all_trt_states {

    local curr_trt_states " `s' "

    local drp_trt_states: list all_trt_states - curr_trt_states

    foreach d of local drp_trt_states {

    drop if state==" `d' "

    }

    gen trt_p1=0

    foreach s1 in " `s' " {

    replace trt_p1=1 if state==" `s1' "
    }

    reg outcome i.trt_p1##i.cohort

    }



    The following line is giving me an error (invalid syntax):
    local curr_trt_states " `s' "

    What am I doing wrong here?

    Thank you in advance.


    Best,
    Amrita Sanyal.

  • #2
    Update:

    I made a mistake in my code and fixed it. Now, I don't get the same error message, but the code doesn't show any results.

    I'm sure the issue is still in the line: local curr_trt_states " `s' ". I'm trying to use an element of a previously defined local in a new local. Is that the problem?

    For instance,

    use Data, clear

    local all_trt_states " "A" "B" "C" " " "D" "E" "F" " " "G" "H" "I" "

    foreach s of local all_trt_states {

    di " `s' "

    }

    works (shows results), but

    use Data, clear

    local all_trt_states " "A" "B" "C" " " "D" "E" "F" " " "G" "H" "I" "

    foreach s of local all_trt_states {

    local curr_trt_states " `s' "

    }

    doesn't.


    Any suggestions would be appreciated.
    Last edited by Amrita Sanyal; 02 Apr 2022, 16:32.

    Comment


    • #3
      No, actually, your mistakes begins right at the top and propagate through the code:

      Code:
      . local all_trt_states " "A" "B" "C" " " "D" "E" "F" " " "G" "H" "I" "
      
      .
      . macro list _all_trt_states
      _all_trt_states:
                      "A" "B" "C" " " "D" "E" "F" " " "G" "H" "I"
      As you can see, the contents of local macro all_trt_states are not what you intended them to be. You do not have three quote-enclosed groups of 3 states each. You have, instead, 11 consecutive strings, two of which, in positions 4 and 8 consist of a single blank space. There is probably some way to fix this with some extremely subtle and clever use of compound double-quotes (See -help quotes- if you are not familiar with those), but it isn't going to be easy or obvious. Stata tends to strip away initial and final quotes from macros when you declare them. Most of the time that works to one's advantage, but not in this case, and working around it is difficult.

      I would use a completely different approach. Something like this:

      Code:
      gen group = 0
      replace group = 1 if inlist(state, "A", "B", "C")
      replace group = 2 if inlist(state, "D", "E", "F")
      replace group = 3 if inlist(state, "G", "H", "I")
      
      gen trt_p1 = (group != 0)
      
      forvalues i = 1/3 {
          regress i.trt_p1##i.cohort if inlist(group, 0, `i')
      }
      Because you did not provide example data, this code is untested, may contain typos or other errors, or even be completely unsuited for your data. If you are not able to adapt it to your actual data and want further assistance, please give representative example data when you post back, and use the -dataex- command to do so. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

      As for your observation that the second block of code in #2 doesn't produce any results, that has nothing to do with your management of the local macros. That's because it doesn't contain any commands that would produce visible results. Your first block there contains a -display- command, but the second one has only commands that are silent when they do not produce error messages.

      Comment


      • #4
        Thank you, Clyde. I will follow your advice on providing data henceforth.
        I understand now why my code wasn't working. I'll try to use your suggested code and see if I can modify it to my needs.

        Best,
        Amrita.

        Comment

        Working...
        X