Announcement

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

  • How to generate a variable from a string variable

    Hey!

    If anyone could help, I'm looking to generate a variable from a string variable which I cant convert into numeric, they are ICD9 codes.

    Ex.

    Gen disease1=0
    replace disease1=1 if hlth_dx_icd9x_code_1==V65.1
    replace disease1=. if hlth_dx_icd9x_code_1==.

    Thanks in advance for all the help!

    Cheers,
    A

    dataex hlth_dx_icd9x_code_1

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str6 hlth_dx_icd9x_code_1
    "780"  
    "783.0"
    "V65.1"
    "825"  
    "V65.1"
    ""     
    "915"  
    "842"  
    "780"  
    "910"  
    "V70"  
    "V65.1"
    "780"  
    "691"  
    "V65.1"
    "009.0"
    "780"  
    "845"  
    "462"  
    "595"  
    "465"  
    "V68"  
    "780"  
    "915"  
    "558"  
    "372.0"
    "780"  
    "078.1"
    "078.1"
    "780"  
    "780"  
    "V20.2"
    "259.1"
    "780"  
    ""     
    "780"  
    "V20"  
    "078.1"
    "259.1"
    "078.1"
    "259.1"
    "780"  
    "078.1"
    "780"  
    "259.1"
    "078.1"
    "780"  
    "V70"  
    "259.1"
    ""     
    ""     
    "259.1"
    ""     
    "780"  
    "259.1"
    "078.1"
    ""     
    "078.1"
    "078.1"
    "616.1"
    "780"  
    "259.1"
    "078.1"
    "V20"  
    "078.1"
    "379.4"
    "780"  
    "078.1"
    "078.1"
    "780"  
    "V20.2"
    "780"  
    "078.1"
    "780"  
    "616.1"
    "848"  
    "845"  
    "780"  
    "786"  
    ""     
    ""     
    ""     
    "V70"  
    "789.0"
    "460"  
    "V70"  
    "787"  
    "034.0"
    ""     
    "782"  
    "780.6"
    "372"  
    "078.1"
    "216.7"
    "V70"  
    "382"  
    "078.1"
    "V70"  
    "487"  
    "078.1"
    end

  • #2
    you need double quotes around the values of the variable and also you indicate missing by using double quotes (with no space between them) instead of a dot; note that if you are worried that there are extraneous spaces, you should eliminate them with "strltrim"; see
    Code:
    help strltrim

    Comment

    Working...
    X