Announcement

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

  • How to generate the column before a column

    Sorry if this has been asked before
    I have dataset containing var1 and var2.
    I want to generate a new variable(var3), such that it matches the column before a specific column (var2, in the following case),where var3=var1
    var1 var2 var3
    123 AB 123
    124 BB 124
    Thanks in advance
    Last edited by Butterfly Poon; 30 Jul 2021, 04:15.

  • #2
    Code:
    generate var3 = var1
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Code:
      help clonevar

      Comment


      • #4
        Originally posted by Maarten Buis View Post
        Code:
        generate var3 = var1
        I actually have several groups of var1 var2 var3,
        so in fact it is var1 var2 var3 var4 var5 var6 var7 var8

        I first hv to identify the maximum value of among var2 var4 var6 var8
        and then i want to match the column in front of the max value and generate a new variable

        Say, the var4 has the max value among var2 var4 var6 var8
        then i want gen(var9), where it is equal to var3


        I am thinking about collapsing var1 var2 in a dataset, and then (var3 and var4) in another.... repeatedly and the generate a new column with merge
        but it seems too complicated

        Comment


        • #5
          You can look at reshaping the data to long format, that should make the task a lot easier. Look at the by prefix maybe combined with egen. I have to pick up the kids now, so I cannot give you more detailed advise.
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment

          Working...
          X