Announcement

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

  • How to convert categorical variables (name of industries) into letters?

    Good morning to the community.

    I would like to convert automatically categorical variables (names of industries) into letters.

    Consumer, Cyclical > CC
    Financial > F
    Communications > C
    Energy > E
    Industrial > I
    Communications > Co
    Consumer, Non-Cyclical > CnC
    Utilities > U
    Technology > T


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str22 industry str3 var2
    "Consumer, Cyclical"     "CC" 
    "Financial"              "F"  
    "Communications"         "C"  
    "Energy"                 "E"  
    "Industrial"             "I"  
    "Communications"         "Co" 
    "Industrial"             "I"  
    "Industrial"             "I"  
    "Consumer, Cyclical"     "CC" 
    "Consumer, Cyclical"     "CC" 
    "Consumer, Non-cyclical" "CnC"
    "Utilities"              ""   
    "Financial"              ""   
    "Consumer, Cyclical"     ""   
    "Financial"              ""   
    "Technology"             ""   
    "Communications"         ""   
    "Consumer, Cyclical"     ""   
    "Financial"              ""   
    "Financial"              ""   
    "Technology"             ""   
    "Consumer, Non-cyclical" ""   
    "Industrial"             ""   
    "Financial"              ""   
    "Financial"              ""   
    "Industrial"             ""   
    "Basic Materials"        ""   
    "Communications"         ""   
    "Industrial"             ""   
    "Industrial"             ""   
    "Consumer, Non-cyclical" ""   
    "Utilities"              ""   
    "Financial"              ""   
    "Consumer, Cyclical"     ""   
    "Consumer, Non-cyclical" ""   
    "Industrial"             ""   
    "Communications"         ""   
    "Industrial"             ""   
    "Basic Materials"        ""   
    "Communications"         ""   
    "Technology"             ""   
    "Industrial"             ""   
    "Financial"              ""   
    "Financial"              ""   
    "Consumer, Non-cyclical" ""   
    "Technology"             ""   
    "Consumer, Non-cyclical" ""   
    "Consumer, Cyclical"     ""   
    "Communications"         ""   
    "Communications"         ""   
    "Communications"         ""   
    "Financial"              ""   
    "Consumer, Non-cyclical" ""   
    "Financial"              ""   
    "Technology"             ""   
    "Industrial"             ""   
    "Industrial"             ""   
    "Consumer, Non-cyclical" ""   
    "Financial"              ""   
    "Consumer, Cyclical"     ""   
    "Communications"         ""   
    "Basic Materials"        ""   
    "Technology"             ""   
    "Industrial"             ""   
    "Industrial"             ""   
    "Consumer, Non-cyclical" ""   
    "Industrial"             ""   
    "Consumer, Non-cyclical" ""   
    "Consumer, Non-cyclical" ""   
    "Financial"              ""   
    "Consumer, Cyclical"     ""   
    "Industrial"             ""   
    "Communications"         ""   
    "Technology"             ""   
    "Utilities"              ""   
    "Utilities"              ""   
    "Consumer, Cyclical"     ""   
    "Financial"              ""   
    "Technology"             ""   
    "Consumer, Cyclical"     ""   
    "Communications"         ""   
    "Industrial"             ""   
    "Communications"         ""   
    "Energy"                 ""   
    "Utilities"              ""   
    "Financial"              ""   
    "Technology"             ""   
    "Financial"              ""   
    "Energy"                 ""   
    "Financial"              ""   
    "Consumer, Cyclical"     ""   
    "Industrial"             ""   
    "Consumer, Cyclical"     ""   
    "Consumer, Cyclical"     ""   
    "Consumer, Cyclical"     ""   
    "Communications"         ""   
    "Industrial"             ""   
    "Industrial"             ""   
    "Basic Materials"        ""   
    "Utilities"              ""   
    end
    Thank you a lot.

    Lucas BORDURE
    Student MSc. in International Finance
    Rennes School of Business
    Stata SE 16.1

  • #2
    Code:
    gen wanted = cond(strpos(industry, ", Cyc"), "CC", cond(strpos(industry, ", Non"), "CnC", cond(strpos(industry, "Comm"), "Co", substr(industry, 1, 1))))
    Writing out several replace statements really isn't wrong!

    Comment


    • #3
      Thank you a lot Nick Cox. The code is working. And it's great to know this formula!

      Lucas BORDURE
      Student MSc. in International Finance
      Rennes School of Business
      Stata SE 16.1

      Comment

      Working...
      X