Announcement

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

  • Making a new variable from the amount of words in data variable

    Sorry if my typing is confusing, could i generate a new variable from counting the amount of words in a certain variable? For example, i have a variable in string that contains of what's everyone favorite's colours, some data has 2 colors or more, and i want to generate a variable where it's a binary variable with 0 for one favorite colors and 1 to data that has more than one favorite colors. Do i just use recode?

  • #2
    This may help.

    If not, please explain what you want that is different using this or another example.

    Other way round, I am not clear how you could use recode here without using some of the code here first.

    This won't work if colours are defined by more than one word. But (e.g.) separation by commas would be easy enough to handle -- with quite different code..


    Code:
    clear
    input str42 colours
    "red"
    "red blue"
    "red blue magenta teal"
    end
    
    gen nwords = wordcount(colours)
    gen morethanone = nwords > 1
    gen MORETHANONE = word(colours, 2) != ""
    
    list
    
         +------------------------------------------------------+
         |               colours   nwords   moreth~e   MORETH~E |
         |------------------------------------------------------|
      1. |                   red        1          0          0 |
      2. |              red blue        2          1          1 |
      3. | red blue magenta teal        4          1          1 |
         +------------------------------------------------------+
    Last edited by Nick Cox; 12 Nov 2023, 10:25.

    Comment


    • #3
      I tried the code and it gives me error type mismatch,
      about recode i was thinking i needed to encode the string var, forgive me if it's not the correct way to do that,
      also to mention, the data in colours has commas (","), and i would like to know if it's gonna be count as one word,

      Comment


      • #4
        If you have a string variable and comma separators, the code will give the wrong result, but not a type mismatch.

        As said, commas as separators require different code.

        As said, please give a data example. #2 showed one and https://www.statalist.org/forums/help#stata explains why and how.

        Comment


        • #5
          I figured it out somehow, thanks Nick!
          In the end i used commands from you and changed the wrong result personally,
          sorry for the lack of data example, i'm using data in another language, i'm afraid i couldn't explain the purpose exactly,
          i'll be sure to use data example next time!

          Comment

          Working...
          X