Announcement

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

  • Encoding string text to numbers

    I have responses to a 5-point likert scale: 1) Strongly disagree 2) Disagree 3) Neither agree nor disagree 4) Agree 5) Strongly Agree. The data is stored as text and what to convert to numeric for all the answers. An example is below:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str26(Ihatethewholeworld Itisbestformetogetyoubef Oftenviolenceistheonlyansw Icancareaboutapersonandst Mypastpainfulexperiencesnol Iamatpeacewithmypast Igainpowerthroughmyassociat)
    "Strongly agree"             "Agree"                      "Neither agree nor disagree" "Neither agree nor disagree" "Neither agree nor disagree" "Neither agree nor disagree" "Agree"                     
    "Strongly agree"             "Agree"                      "Strongly agree"             "Strongly agree"             "Strongly agree"             "Agree"                      "Agree"                     
    "Strongly agree"             "Strongly agree"             "Agree"                      "Neither agree nor disagree" "Agree"                      "Strongly agree"             "Strongly agree"            
    "Agree"                      "Strongly agree"             "Strongly agree"             "Neither agree nor disagree" "Strongly agree"             "Strongly agree"             "Strongly agree"            
    "Strongly agree"             "Agree"                      "Neither agree nor disagree" "Agree"                      "Neither agree nor disagree" "Neither agree nor disagree" "Strongly agree"            
    "Agree"                      "Agree"                      "Agree"                      "Agree"                      "Agree"                      "Agree"                      "Agree"                     
    "Agree"                      "Agree"                      "Agree"                      "Agree"                      "Strongly agree"             "Agree"                      "Agree"                     
    "Strongly agree"             "Strongly agree"             "Neither agree nor disagree" "Strongly agree"             "Agree"                      "Agree"                      "Neither agree nor disagree"
    "Strongly agree"             "Agree"                      "Agree"                      "Neither agree nor disagree" "Neither agree nor disagree" "Agree"                      "Strongly agree"            
    "Agree"                      "Agree"                      "Strongly agree"             "Agree"                      "Agree"                      "Strongly agree"             "Agree"                     
    "Neither agree nor disagree" "Neither agree nor disagree" "Neither agree nor disagree" "Neither agree nor disagree" "Agree"                      "Agree"                      "Neither agree nor disagree"
    "Agree"                      "Agree"                      "Agree"                      "Agree"                      "Agree"                      "Agree"                      "Strongly agree"            
    "Neither agree nor disagree" "Neither agree nor disagree" "Agree"                      "Neither agree nor disagree" "Strongly agree"             "Disagree"                   "Neither agree nor disagree"
    "Neither agree nor disagree" "Strongly agree"             "Strongly agree"             "Neither agree nor disagree" "Neither agree nor disagree" "Agree"                      "Strongly agree"            
    "Agree"                      "Agree"                      "Strongly agree"             "Neither agree nor disagree" "Strongly agree"             "Strongly agree"             "Agree"                     
    "Disagree"                   "Neither agree nor disagree" "Disagree"                   "Neither agree nor disagree" "Agree"                      "Agree"                      "Neither agree nor disagree"
    "Disagree"                   "Neither agree nor disagree" "Neither agree nor disagree" "Agree"                      "Agree"                      "Agree"                      "Neither agree nor disagree"
    "Neither agree nor disagree" "Disagree"                   "Disagree"                   "Agree"                      "Neither agree nor disagree" "Agree"                      "Agree"                     
    "Agree"                      "Agree"                      "Agree"                      "Agree"                      "Neither agree nor disagree" "Agree"                      "Neither agree nor disagree"
    "Strongly agree"             "Strongly agree"             "Agree"                      "Strongly agree"             "Strongly agree"             "Agree"                      "Strongly agree"            
    "Disagree"                   "Neither agree nor disagree" "Disagree"                   "Agree"                      "Agree"                      "Agree"                      "Neither agree nor disagree"
    "Neither agree nor disagree" "Agree"                      "Agree"                      "Agree"                      "Agree"                      "Agree"                      "Disagree"                  
    "Agree"                      "Agree"                      "Agree"                      "Agree"                      "Neither agree nor disagree" "Agree"                      "Neither agree nor disagree"
    "Disagree"                   "Neither agree nor disagree" "Neither agree nor disagree" "Neither agree nor disagree" "Disagree"                   "Neither agree nor disagree" "Neither agree nor disagree"
    "Agree"                      "Agree"                      "Strongly agree"             "Agree"                      "Agree"                      "Strongly agree"             "Strongly agree"            
    "Agree"                      "Agree"                      "Neither agree nor disagree" "Neither agree nor disagree" "Agree"                      "Agree"                      "Neither agree nor disagree"
    "Strongly agree"             "Agree"                      "Strongly agree"             "Agree"                      "Agree"                      "Agree"                      "Strongly agree"            
    "Neither agree nor disagree" "Agree"                      "Neither agree nor disagree" "Neither agree nor disagree" "Agree"                      "Neither agree nor disagree" "Agree"                     
    "Agree"                      "Agree"                      "Agree"                      "Strongly agree"             "Agree"                      "Agree"                      "Strongly agree"            
    "Agree"                      "Agree"                      "Neither agree nor disagree" "Agree"                      "Strongly agree"             "Neither agree nor disagree" "Neither agree nor disagree"
    "Disagree"                   "Neither agree nor disagree" "Agree"                      "Agree"                      "Strongly agree"             "Agree"                      "Agree"                     
    "Strongly agree"             "Agree"                      "Agree"                      "Agree"                      "Strongly agree"             "Strongly agree"             "Agree"                     
    "Neither agree nor disagree" "Strongly agree"             "Strongly disagree"          "Neither agree nor disagree" "Strongly agree"             "Agree"                      "Agree"                     
    "Neither agree nor disagree" "Neither agree nor disagree" "Neither agree nor disagree" "Neither agree nor disagree" "Agree"                      "Disagree"                   "Disagree"                  
    "Neither agree nor disagree" "Agree"                      "Agree"                      "Neither agree nor disagree" "Neither agree nor disagree" "Neither agree nor disagree" "Neither agree nor disagree"
    "Agree"                      "Agree"                      "Strongly agree"             "Agree"                      "Disagree"                   "Agree"                      "Disagree"                  
    "Disagree"                   "Disagree"                   "Disagree"                   "Agree"                      "Agree"                      "Neither agree nor disagree" "Neither agree nor disagree"
    "Agree"                      "Strongly agree"             "Strongly agree"             "Strongly agree"             "Agree"                      "Agree"                      "Strongly agree"            
    "Neither agree nor disagree" "Agree"                      "Disagree"                   "Agree"                      "Neither agree nor disagree" "Agree"                      "Neither agree nor disagree"
    "Agree"                      "Agree"                      "Strongly agree"             "Disagree"                   "Strongly agree"             "Agree"                      "Strongly agree"            
    "Strongly disagree"          "Neither agree nor disagree" "Neither agree nor disagree" "Neither agree nor disagree" "Disagree"                   "Neither agree nor disagree" "Agree"                     
    "Neither agree nor disagree" "Agree"                      "Disagree"                   "Neither agree nor disagree" "Agree"                      "Agree"                      "Neither agree nor disagree"
    "Neither agree nor disagree" "Agree"                      "Agree"                      "Agree"                      "Agree"                      "Agree"                      "Agree"                     
    "Agree"                      "Strongly agree"             "Agree"                      "Strongly agree"             "Strongly agree"             "Agree"                      "Agree"                     
    "Agree"                      "Agree"                      "Strongly agree"             "Strongly agree"             "Strongly agree"             "Agree"                      "Agree"                     
    "Strongly agree"             "Neither agree nor disagree" "Neither agree nor disagree" "Neither agree nor disagree" "Neither agree nor disagree" "Neither agree nor disagree" "Strongly agree"            
    "Strongly agree"             "Agree"                      "Neither agree nor disagree" "Strongly agree"             "Agree"                      "Neither agree nor disagree" "Disagree"                  
    "Agree"                      "Agree"                      "Agree"                      "Neither agree nor disagree" "Agree"                      "Agree"                      "Strongly agree"            
    "Disagree"                   "Agree"                      "Disagree"                   "Agree"                      "Disagree"                   "Agree"                      "Agree"                     
    "Disagree"                   "Agree"                      "Neither agree nor disagree" "Disagree"                   "Neither agree nor disagree" "Agree"                      "Agree"                     
    end

  • #2
    These are classic Likert scale items. The key here is to define the label so that the order of the numbers corresponds to the intensity of agreement.

    Code:
    label define likert    1    "Strongly disagree" ///
                        2    "Disagree"    ///
                        3    "Neither agree nor disagree" ///
                        4    "Agree"    ///
                        5    "Strongly agree"
                        
    foreach v of varlist Ihatethewholeworld-Igainpowerthroughmyassociat {
        encode `v', gen(_`v') label(likert) noextend
        drop `v'
        rename _`v' `v'
    }

    Comment


    • #3
      Consider seeing help lab define
      In essence, you define your labels with their value labels and then label the variable with those values/value labels. It should be pretty simple.
      Some variables don't seem to use all 5 rating levels. Think over what values you would want to pass for each rating level string in those instances. See code below that will do this check for you.


      Code:
      ds
      foreach v in `r(varlist)'{
          qui{
              levelsof `v'
               ret li
          }
          if r(r)==4 {
              di "`v' has only `r(r)' levels"
          }
          else if r(r)==5 {
              di "`v' has `r(r)' levels"
          }
      }

      Comment


      • #4
        Crossed with #2 from Clyde Schechter, you have what you need. Although I was not aware of the noextend option. Will have to read that up.

        Comment


        • #5
          The -noextend- option tells Stata that if during the -encode-ing it encounters a value that is not included in the label it should not extend the label to include that value as a label (with the next available sequential integer), and it should give an error message. It is an indirect way of having Stata verify that each of the variables contains only the values that we expect to find in that variable. One might normally do that with -assert inlist(`v', "Strongly disagree", "Disagree", "Neither agree nor disagree", "Agree", "Strongly agree")-, but just -noextend- is a lot easier to type!

          Comment


          • #6
            Originally posted by Clyde Schechter View Post
            The -noextend- option tells Stata that if during the -encode-ing it encounters a value that is not included in the label it should not extend the label to include that value as a label (with the next available sequential integer), and it should give an error message. It is an indirect way of having Stata verify that each of the variables contains only the values that we expect to find in that variable. One might normally do that with -assert inlist(`v', "Strongly disagree", "Disagree", "Neither agree nor disagree", "Agree", "Strongly agree")-, but just -noextend- is a lot easier to type!
            Love it. Learnt something new in a seemingly simple command to improve my workflow. Thanks as always, Clyde Schechter for taking the extra time to explain.

            Comment


            • #7
              Some adjuncts:

              distinct from the Stata Journal counts the number of distinct values -- without reference to what they are.

              findname from the Stata Journal can check for all values being 1 to 5 or for any values not 1 to 5.

              Code:
              findname, all(inrange(@, 1, 5)) 
              findname, all(inlist(@, 1, 2, 3, 4, 5)) 
              findname, any(!inlist(@, 1, 2, 3, 4, 5))
              multencode from SSC can encode several variables without a loop. It doesn't undo the need to define value labels first for this problem.

              Comment


              • #8
                Thank you for the excellent solution. Best regards!

                Comment

                Working...
                X