Announcement

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

  • Creating a dummy that detects any string variable

    Dear Statalists,

    My data set has a "name" variable, which is a string variable with individual names. However, there are many missing names in the list (i.e., zero string). I would like to create a dummy variable that shows 1 for any name, and 0 for missing. How can I create a code for that?

    I tried
    gen namedummy=.
    replace namedummy =1 if name==" "

    I got "0 changes made".


  • #2
    Code:
    generate byte missing_name = missing(name)
    or
    Code:
    generate byte missing_name = name == ""

    Comment


    • #3
      If you have faithfully transcribed your code here, you went wrong with the name==" " part. The "zero" string is not a blank space, it is a null: so you want name == "".

      However, a simpler approach is:

      Code:
      gen namedummy = !missing(name)
      Note also that the code I'm showing here will generate a 0/1 variable (which is what you said you want), whereas your code, had it worked at all, would have produced a ./1 variable. ./1 variables in Stata are a recipe for errors down the road. Stata's logical operators are nicely equipped to deal with 0/1 variables. With ./1 variables, not so much, because . and 1 are both taken to be true in boolean expressions.

      Added: Crossed with #2 which makes the same points.

      Comment


      • #4
        Your code would normally work if the string is truly empty (even though it would give you a 1 for a missing value of name and 0 for a non-missing value). There might be some spaces in your name variable that you can't see though. Try trimming it first, like this:

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str21 notaname
        " "                    
        "Cdyqbqasai Austjfaisv"
        "Saaxulzxlw Awxfytmqqf"
        "Ogkkhudbce Ssdbgqpqov"
        "Rhtckemioi Twdtmpwsuu"
        "  "                    
        "Bmodqoxbuv Nhuanpcrao"
        "Nynquntmaj Waydyabmzh"
        "Epwuvjxpoo Bxqjsrtqtm"
        "Plfgniwolf Wkbeflufyp"
        "   "                    
        "Sobjrjoufv Hdlpucvbbh"
        "Njcresrrzj Cejliozfoq"
        "Zlijoquynj Dzlmoeacqs"
        "Zyldxjdnkw Netofygmac"
        "    "                    
        "Zvextwaenc Njwyedjjsj"
        "Qeyppoxepu Spmdxwncfw"
        "Fedubvykbi Cmazwhkwjg"
        "Qdmkzygzcx Kdugzwoeqi"
        "     "                    
        "Ivkcpfiecw Jzxjmddjyo"
        "Jnlmdqzdxm Tajwbuysxc"
        "Cuattqvdrh Qcwvyismhd"
        "Numixddstu Vgrirsplaj"
        "      "                    
        "Oatrmputvx Fgckpduuab"
        "Uehcrfsjwa Bjevpohrdo"
        "Yhjjqhwmeb Gnjtaxrrws"
        "Ndaokzugot Gvzxrqeiap"
        "       "                    
        "Yzfaclhbrw Rvoqdvnvmt"
        "Zwyjawcxwm Bcemkeacoa"
        "Svlnkzodvd Ozfjtpjqut"
        "Predlgxysx Exayslsjth"
        end
        
        replace notaname=trim(notaname)
        gen namedummy = notaname!=""
        If that doesn't work you should share a snippet of your data using dataex (if you don't have it already, type ssc install dataex to install it and then read help dataex to learn how to use it)


        Comment


        • #5
          Thank you everyone. I tried Clyde's code, and it works great!

          Comment

          Working...
          X