Announcement

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

  • Help on correcting typos in a string variable

    Hello there,


    I was struggling with the following problem in stata:


    I have a string variable "bench" which represents city where a court hearing took place. I want to make a dummy variable for each of the cities which represents each of the bench string e.g. one dummy for "isl" one for "lar" etc. This I know, I can do by using:


    tab bench, gen(city)


    However, there are some typos in my string variable bench. For example, the same city Islamabad is sometimes coded as "isl", "isb" and "isd". Naturally, making dummy variable based on my old string will give me extra dummies on the same Islamabad city.


    So, how can I rename the string so that I can have one dummy variable per city.


    I tried:


    if bench == "isd" | "isb" == 1 {

    replace bench = "isl"

    }



    But this did not work. How should I proceed to solve this problem? Thank you in advance.


    Kind Regards,

    Sultan
    Last edited by Sultan Mehmood; 28 Oct 2016, 08:49.

  • #2
    Two problems: you need the -if- qualifier, not the -if- command. And the condition itself is improperly specified. What you want its:

    Code:
    replace bench = "isl" if bench == "isd" | bench == "isb"
    which, in fact, can be shortened and simplified somewhat to:

    Code:
    replace bench = "isl" if inlist(bench, "isd", "isb")
    It is important in Stata to understand the difference between an -if- qualifier which restricts the subset of the data to which a command will be applied, and an -if- command which determines whether some global condition is true and then determines whether a command, or block of commands, gets executed at all. See -help if- and -help ifcmd- and the corresponding manual sections. Mastering the difference between them will save you a world of headaches.
    Last edited by Clyde Schechter; 28 Oct 2016, 09:14. Reason: Correct typo.

    Comment


    • #3
      Thank you very much!

      Comment

      Working...
      X