Announcement

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

  • "Encode" a String with another String?

    Hello:
    I am trying to create a long code list where var1 is a string and what it should represent is shown in var2, also a string.
    I then would like to store this coding so that I can refer to it later when var1 shows up, var2 will be displayed in the dataset.
    This is in a similar fashion to how I would think -encode- would work for strings to a number, except I want to get another string.

    Code:
    *
    clear
    input str5 var1 str27 var2
    "one"   "the diameter of the earth"  
    "two"   "the diameter of venus"      
    "three" "the diameter of jupiter"    
    "four"  "the population of paris"    
    "five"  "the surface area of england"
    "six"   "the population of phoenix"  
    end
    Is there any sort of command available for this?
    Or if not, I thought to try to make a loop using a global, but couldn't get the syntax right. I have a few thousand unique strings I'd like to "encode", is there a limit to the number of globals you can have?

    Thanks for advice...

  • #2
    Leonard, there is no easy way of creating the original list, and you may have to complete the list line by line. When finishing the list, you may save it as a new dataset, for example, "list.dta". Then, whenever you open another dataset with var1 only and would like var2 to appear too, just use merge to combine your current data and "list.dta".

    Comment


    • #3
      Another way is to label var1 with values of var2. But as var1 is string, value labels can't be created. So when you create the list, try inputting 1, 2, 3, ... instead of "one", "two", "three",... in var1. Then you may define a value label from the list data and assign it to var1 in any other datasets.

      Code:
      clear
      input var1 str27 var2
      1   "the diameter of the earth"  
      2   "the diameter of venus"      
      3 "the diameter of jupiter"    
      4  "the population of paris"    
      5  "the surface area of england"
      6   "the population of phoenix"  
      end
      
      labmask var1, values(var2)
      lab list var1

      Comment


      • #4
        labmask is from the Stata Journal, so you need to install before you can use it.

        Code:
        search labmask, sj
        and then click on
        gr0034

        Comment

        Working...
        X