Announcement

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

  • Dealing with a mixture of numeric and character variables

    I have a dataset where some of the columns contain a mix of numbers (eg. 1-5) and characters (eg. Don't know and Not applicable). I was wondering if anyone knows how I can change the words into a number using STATA. I'm using STATA 13. I haven't had success with destring or encode. I also tried to get rid of the space in Not applicable using itrim() and that hasn't worked for me either. Thanks so much!

  • #2
    Please study the FAQ Advice, especially sections 12 and 18.

    We can't see what code you tried with destring or encode

    itrim() is not for removing internal spaces, but for reducing multiple internal spaces to single spaces.

    This may show some technique.

    Code:
     
    clear
    input str14 response
    "1"
    "2"
    "3"
    "4"
    "5"
    "Don't know"
    "Not applicable"
    end
    replace response = ".a" if response == "Don't know"
    replace response = ".b" if response == "Not applicable"
    label def mylabels 1 "strongly disagree" 2 "disagree" 3 "neutral" 4 "agree" 5 "strongly agree" ///
    .a "Don't know" .b "Not applicable"
    destring response, replace
    label val response mylabels
    
    . describe
    
    Contains data
    obs: 7
    vars: 1
    size: 35 (99.9% of memory free)
    ---------------------------------------------------------------------------------
    storage display value
    variable name type format label variable label
    ---------------------------------------------------------------------------------
    response byte %17.0g mylabels
    ---------------------------------------------------------------------------------
    Sorted by:
    Note: dataset has changed since last saved
    
    . list
    
    +-------------------+
    | response |
    |-------------------|
    1. | strongly disagree |
    2. | disagree |
    3. | neutral |
    4. | agree |
    5. | strongly agree |
    |-------------------|
    6. | Don't know |
    7. | Not applicable |
    +-------------------+

    Comment


    • #3
      Thank you for the suggestion. The code replace response = ".a" if response == "Not applicable" worked.

      Comment

      Working...
      X