Announcement

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

  • Creating new variable based on diffrences in other variables

    Hi!

    I hope you can help me with a clever solution so I can skip the manual generation of new variables in a large dataset.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(ID SampleID Category_1 Category_2)
    1 1 1  4
    1 1 2 37
    1 2 2 50
    2 1 1  6
    2 2 1  6
    2 2 1  6
    2 2 1  6
    2 2 1  6
    3 1 4 81
    4 1 1  5
    5 1 1 21
    5 2 1 21
    5 3 1 21
    5 4 1 21
    6 1 2 43
    6 1 2 76
    end
    I want to create two new variables.

    1. Within the same ID and SampleID, if Category_1 observations are all the same, the value in the new variable (New_Category_1) should be 1, 2, 3 or 4. Within the same ID and SampleID, if Category_1 contains different values, the value in New_Category_1 should be 5.

    2. Same as above, only Category 2 can contain up to 100 different values, and the New_Category_2 should only have 2 possible outcomes (simple or complex)

    I hope this makes sense.

    Thank you.

  • #2
    Code:
    bys ID SampleID (Category_1): gen byte New_Category_1 = cond(Category_1[1] == Category_1[_N], Category_1, 5)
    bys ID SampleID (Category_2): gen New_Category_2 = cond(Category_2[1] == Category_2[_N], "simple", "complex")
    which produces:

    Code:
    . list, noobs sep(0)
    
      +-----------------------------------------------------------+
      | ID   SampleID   Catego~1   Catego~2   New_Ca~1   New_Ca~2 |
      |-----------------------------------------------------------|
      |  1          1          1          4          5    complex |
      |  1          1          2         37          5    complex |
      |  1          2          2         50          2     simple |
      |  2          1          1          6          1     simple |
      |  2          2          1          6          1     simple |
      |  2          2          1          6          1     simple |
      |  2          2          1          6          1     simple |
      |  2          2          1          6          1     simple |
      |  3          1          4         81          4     simple |
      |  4          1          1          5          1     simple |
      |  5          1          1         21          1     simple |
      |  5          2          1         21          1     simple |
      |  5          3          1         21          1     simple |
      |  5          4          1         21          1     simple |
      |  6          1          2         43          2    complex |
      |  6          1          2         76          2    complex |
      +-----------------------------------------------------------+

    Comment


    • #3
      Worked perfectly, thank you!

      Comment


      • #4
        Worked perfectly, thank you!

        Comment

        Working...
        X