Announcement

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

  • Encode so that variables take on same values across similar variables

    Hi all,

    I'm attempting to recode several string variables. Specifically, I have 5 variables representing industries, ie, Industry1, Industry2 Industry3, etc. Possible values for each variable are the same (eg, Construction, Manufacturing, Health Care, etc). Because the "encode" command assigns values to string observations alphabetically, in "Industry1", Construction may be coded as 1, but if Construction does not appear in the variable "Industry2", Health Care may be coded as 1.

    Is there an efficient way to encode all 5 variables so that the same string value takes on the same numeric value across all 5?

    Thank you in advance!

  • #2
    Code:
    ssc desc multencode 
    
    
    ---------------------------------------------------------------------------------
    package multencode from http://fmwww.bc.edu/repec/bocode/m
    ---------------------------------------------------------------------------------
    
    TITLE
          'MULTENCODE': module to encode multiple string variables into numeric
    
    DESCRIPTION/AUTHOR(S)
          
          multencode creates new numeric variables newvarlist, with value
          labels defined and attached, based on the string variables
          strvarlist. The same set of value labels is used for all the new
          variables.
          
          KW: encode
          KW: string
          KW: numeric
          KW: value labels
          
          Requires: Stata version 9
          
          Distribution-Date: 20090121
          
          Author: Nicholas J. Cox, Durham University
          Support: email [email protected]
          
    
    INSTALLATION FILES                           (type net install multencode)
          multencode.ado
          multencode.hlp
    ---------------------------------------------------------------------------------
    (type -ssc install multencode- to install)

    Comment


    • #3
      Thank you so much. I'll be sure to remember this one!

      Comment


      • #4
        No disrespect to Nick's solution, but you can do this directly using the -label()- option of -encode-

        Code:
        encode stringvar1, gen(numvar1) label(codes)
        encode stringvar2, gen(numvar2) label(codes)
        The first use, the label -codes- is created. The second time it is reused, and extended as necessary. This ensures that all the variables share the same labels.

        hth,
        Jeph

        Comment


        • #5
          Jeph's idea is neat and uses just official commands. The downside is likely to be an untidy set of labels. If stringvar1 is based on frog, newt and toad and dragon isn't seen before stringvar2 then the labels will be in that order.

          Comment

          Working...
          X