Announcement

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

  • Creating a string variable with multiple different word values

    Hello,

    I am relatively new to STATA, with only a couple months experience and not much string experience.

    I want to create a variable that stores 10 separate different words.

    Ex:
    animal:
    cat
    dog
    lizard
    bird
    etc

    I tried doing something like
    gen animal = "cat" + " " + "dog"....

    but that just gave me:

    animal = cat dog lizard bird

    and the results aren't all split up. I am doing this so that I can basically use this string value as the qualifier for an f statement that will do a command when the string is equal to one of the word values.

    Thanks!

  • #2
    With an empty dataset, you could
    Code:
    input str250 animal
    cat
    dog
    lizard
    bird
    etc
    end
    The string length is set arbitrarily large in order to accommodate any lengthy animal names. (Afterward, you can use compress if memory is at a premium.) If any of your animals above had spaces, you'd need to surround it by double-quotation marks.

    With this dataset of animals, you could merge or cross (or other command of this nature) with another dataset on which you want to use the if statement or command qualifier.

    Comment


    • #3
      Please read the FAQ. There you'll find the best approach to share data/output/command.

      This is the best way to entail an insightful reply.
      Best regards,

      Marcos

      Comment


      • #4
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str10 animal float is_mammal
        "cat"        1
        "dog"        1
        "lizard"     0
        "bird"       0
        "lion"       1
        "tiger"      1
        "bear"       1
        "Black bear" 1
        "Brown Bear" 0
        end
        So, once you get the string data entered that you need, this is how you can do a command (in this case, set variable is_mammal==1) when the string is equal to one of the word values.

        Code:
        gen is_mammal = 0
        replace is_mammal = 1 if strpos(animal, "cat") > 0
        
        * If you have many, you might want to do a loop:
        foreach name in "dog" "bear" "deer" "lion" "tiger" {
        replace is_mammal = 1 if strpos(animal, "`name'") > 0
        }
        
        . list animal is_mammal
        
             +-----------------------+
             |     animal   is_mam~l |
             |-----------------------|
          1. |        cat          1 |
          2. |        dog          1 |
          3. |     lizard          0 |
          4. |       bird          0 |
          5. |       lion          1 |
             |-----------------------|
          6. |      tiger          1 |
          7. |       bear          1 |
          8. | Black bear          1 |
          9. | Brown Bear          0 |
             +-----------------------+
        Note that the string function strpos is case sensitive, so strpos(animal, "bear") matched to "bear" and "Black bear" but not "Brown Bear". Use the string function strupper() to convert everything to upper case if matching on case is an issue.
        Last edited by David Benson; 28 Nov 2018, 23:55.

        Comment


        • #5
          I read #1 differently from some previous answers. I interpret your goal -- slight editing here -- as

          I want to use this string value as the qualifier for an if statement that will do a command when a string variable is equal to one of the word values
          You can always go

          Code:
          ... if animal == "cat"
          and there is no obvious need for or value in putting "cat" in a variable with other possibilities.

          Comment

          Working...
          X