Announcement

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

  • Generating new variables based on relative alphabetical position of other variables

    Hello!

    I am looking to generate two new string variables based on the relative alphabetical position of two other variables, wherein the first new variable comes first alphabetically and the second comes second. For an example I would like to turn this:

    V1 V2
    Apple Orange
    Tree Biscuit
    Sad Happy
    Beef Brain

    into this:

    V3 V4
    Apple Orange
    Biscuit Tree
    Happy Sad
    Beef Brain

    in Stata. Does anyone have any suggestions?
    Last edited by Robert Pettis; 04 May 2018, 16:24.

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str5 v1 str7 v2
    "Apple" "Orange" 
    "Tree"  "Biscuit"
    "Sad"   "Happy"  
    "Beef"  "Brain"  
    end
    
    list
    
    gen v3 = cond(v1 < v2, v1, v2)
    gen v4 = cond(v2 <= v1, v1, v2)
    
    list
    In the future, when showing data examples, please use the -dataex- command to do so, as I have done in this reply. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      This appears to do the trick. Thank you for the help and for informing me about dataex.

      Comment

      Working...
      X