Announcement

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

  • Creating a subset of observations within a variable

    Hello everyone,

    I am facing an issue with coding. What I want to do is create three groups of string observations (variable "country") based on the strength of voting enforcement: strong, weak and voluntary. I have a group of countries that would correspond to each variable. My idea was to code it as follows:

    gen voting_enforcement = ""
    replace voting_enforcement = "strong" if country == "Argentina" | country == "Brazil" | country == "Chile" | country == "Bolivia" | country == "Ecuador" | country == "Uruguay" | country == "Peru"

    But it says there is a type mismatch, which means that I am combining variables that cannot be combined. I don't understand how I can do it in some other way.

  • #2
    Originally posted by Anna Lipiec View Post
    . . . it says there is a type mismatch, which means that I am combining variables that cannot be combined.
    It more likely means that your country variable is numeric, an integer with value labels for the names of the countries.

    I don't understand how I can do it in some other way.
    You can try something like the following.
    Code:
    quietly generate str voting_enforcement = ""
    
    foreach country in Argentina Brazil Chile Bolivia Ecuador Uruguay Peru {
        quietly replace voting_enforcement = "strong" ///
            if country == "`country'":`: value label country'
    }

    Comment


    • #3
      Okay, I tried this code and the following pops up: "program error: code follows on the same line as open brace"

      Comment


      • #4
        Originally posted by Anna Lipiec View Post
        . . .the following pops up: "program error: code follows on the same line as open brace"
        You seemed to have miscopied the code such that something like the following ended up being what you entered in Stata.
        Code:
        foreach country in Argentina Brazil Chile Bolivia Ecuador Uruguay Peru { quietly replace voting_enforcement . . .
        Copy the code more carefully. I've attached a do-file (countries.do) and its output log file (countries.smcl) that illustrate the method. You can run the do-file or examine the log file in order to see how the code ought to appear.
        Attached Files

        Comment

        Working...
        X