Announcement

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

  • Trouble Creating a Dummy Variable

    Hi all, thanks in advance for the help:

    I am rather new to Stata, and I am completing my thesis.

    I am trying to create a dummy variable out of a list of city names, with some cities being in the treatment group and some being control.

    My first thought was to do the following:

    gen Treatment=.

    encode Municipality, gen(municipalityCoded)

    replace Treatment=1 if municipalityCoded=="Montreal" *whatever the label name was Montreal for an example*


    Then I figured I would do the same for the other cities (I am open to learning a way to do it for multiple cities at once) but it just doesn't work, telling me there is a type mismatch.

    Any help would be extremely appreciated as I am a novice

  • #2
    I believe this YouTube video will help with what you're doing: https://www.youtube.com/watch?v=KdfP...esearchSupport

    Obviously you will need to adapt it to your data but I believe the premise is there.

    Comment


    • #3
      You need to show a datasample so we know what is categorical and what is numeric. But generally your code would be something like:

      Code:
      gen Treatment = Municipality==something | Municipality==somethingelse

      Comment


      • #4
        Michael, the reason your -replace- command gives an error is that the -encode- command generates a numeric variable municipalityCoded, which is labeled with the strings of the original variable Municipality.

        The "value labels" of this new numeric variable by default have the same name as the variable, i.e. municipalityCoded. You can use these labels in your replace command as follows:

        Code:
        replace Treatment=1 if municipalityCoded=="Montreal":municipalityCoded
        where the bit after the colon is the name of the value label (in this case, the same as the name of the variable).

        That said, you may not need to create individual dummy variables manually at all. Many commands (like regress) can automatically create these for their purposes. See

        Code:
        help factor variables

        Comment

        Working...
        X