Announcement

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

  • Re-arrange variables

    Dear all,

    I have 30 variables in different columns. Now I want Stata to generate one new variable with all the 30 variables arranged below. Can somebody tell me the command for that operation?

    Best regards,

    CM

  • #2
    Probably what you have on your mind is -reshape-.

    Comment


    • #3
      I have got the following structure:
      x1 x2 x3
      1 2 3
      1 2 3
      1 2 3
      1 2 3
      2
      2
      How can I get this below?
      x4
      1
      1
      1
      1
      2
      2
      2
      2
      2
      2
      3
      3
      3
      3
      Thank you in advance!
      Last edited by corny mueller; 10 Jan 2019, 17:02.

      Comment


      • #4
        Code:
        . gen id = _n
        
        . reshape long x, i(id) j(num)
        (note: j = 1 2 3)
        
        Data                               wide   ->   long
        -----------------------------------------------------------------------------
        Number of obs.                        6   ->      18
        Number of variables                   4   ->       3
        j variable (3 values)                     ->   num
        xij variables:
                                       x1 x2 x3   ->   x
        -----------------------------------------------------------------------------
        
        . sort num id
        
        . list , clean
        
               id   num   x  
          1.    1     1   1  
          2.    2     1   1  
          3.    3     1   1  
          4.    4     1   1  
          5.    5     1   .  
          6.    6     1   .  
          7.    1     2   2  
          8.    2     2   2  
          9.    3     2   2  
         10.    4     2   2  
         11.    5     2   2  
         12.    6     2   2  
         13.    1     3   3  
         14.    2     3   3  
         15.    3     3   3  
         16.    4     3   3  
         17.    5     3   .  
         18.    6     3   .

        Comment


        • #5
          And you should not do that, you should preserve your structure and keep the empty slots whereever there were missing variables, but
          if you want not to have the missing values:

          Code:
          . drop if missing(x)
          (4 observations deleted)
          
          . list , clean
          
                 id   num   x  
            1.    1     1   1  
            2.    2     1   1  
            3.    3     1   1  
            4.    4     1   1  
            5.    1     2   2  
            6.    2     2   2  
            7.    3     2   2  
            8.    4     2   2  
            9.    5     2   2  
           10.    6     2   2  
           11.    1     3   3  
           12.    2     3   3  
           13.    3     3   3  
           14.    4     3   3

          Comment

          Working...
          X