Announcement

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

  • Efficient recoding for long measures

    Howdy!

    I've been tasked with cleaning and recoding a dataset.

    I've come across two sets of measures with 84 items. All items are stored as a strings and I am converting them to numeric for analysis.

    My current code looks like this, and I will be adapting this structure code for ccnes1 to ccnes84.:

    *CCNES1*
    replace ccnes1 = "." if ccnes1 == ""
    replace ccnes1 = "1" if ccnes1 == "(Very unlikely)1"
    replace ccnes1 = "2" if ccnes1 == "2"
    replace ccnes1 = "3" if ccnes1 == "3"
    replace ccnes1 = "4" if ccnes1 == "(Neither likely nor unlikely)4"
    replace ccnes1 = "5" if ccnes1 == "5"
    replace ccnes1 = "6" if ccnes1 == "6"
    replace ccnes1 = "7" if ccnes1 == "(Very likely)7"
    *Converting from string to numeric
    destring ccnes1, gen(ccnes1_n)
    *Dropping old variable
    drop ccnes1
    *Renaming new variablepbace8
    rename ccnes1_n ccnes1
    *Labelling new variable
    lab def ccnes 1 "(Very unlikely) 1" 2 "2" 3 "3" 4 "(Neither likely nor unlikely) 4" 5 "5" 6 "6" 7 "(Very likely) 7"
    lab val ccnes1 ccnes
    *Checking variable
    codebook ccnes1

    Is there a more efficient way to recode, and relabel, rather than using the above code 84 different times for the individual questions in the measure?

    Thanks in advance hivemind!

  • #2
    Yes. In fact, I see no reason to use the approach you have taken as this can be greatly streamlined with the -encode- command.

    I will make the assumption that your 84 variables are all of the form ccnes*. If not, hopefully the whole list of them can be written in a few wildcard expressions that you can use instead of ccnes* in the code shown below.

    The whole thing, all 84 variables, can be done in two lines of code.

    Code:
    lab def ccnes 1 "(Very unlikely) 1" 2 "2" 3 "3" 4 "(Neither likely nor unlikely) 4" 5 "5" 6 "6" 7 "(Very likely) 7"
    encoderall ccnes*, label(ccnes)
    -encoderall- is found in the -encoder- package by Daniel Klein, available from SSC.

    Note: I notice that in your -replace- commands you have things like "(Very unlikely)1" with no blank space between the ) and the following number. If that is accurate, then the -lab def- command must do likewise for this code to work, the text in the value labels must exactly match the text in the original ccnes* text variables. So modify the code accordingly if that is the case. If you want the ultimate results to have a space there, then you can fix the label after the fact with
    Code:
    lab def ccnes 1 "(Very unlikely) 1"  4 "(Neither likely nor unlikely) 4"   7 "(Very likely) 7", modify
    and the change will automatically apply to all the variables you applied the label to (or subsequently apply the label to)

    Note: No example data was given, so the code is untested. Beware of typos or other errors.
    Last edited by Clyde Schechter; 23 May 2022, 20:44.

    Comment


    • #3
      Much obliged Clyde! Thank you kindly

      Comment

      Working...
      X