Announcement

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

  • Append ignoring different variable names

    I have a big dataset that I need to append: Dataset1 and Dataset2.
    I need to append Dataset2 from Dataset1.

    Dataset1 only has the names in the firstrow (i.e. country1_2000, country1_2001,...country1_2020, country2_2000,...country2_2020,...)
    Dataset2 has arbitrary variable names with the actual data (v1,v2,......)

    I need to append Dataset2 to Dataset1 while keeping the variable names.

    Is there a way to do this? Basically, I want the Stata to append exactly column by column.
    For example column1 in Dataset2 to column1 in Dataset1.






  • #2
    Both -append- and -frameappend- will try to correlate common variable names, or else treat distinct variables from each dataset as separate variables. If I understand your post correct, you want variables to be appended in the same numeric order as they exist in each dataset. You can approximate this behaviour using the above commands by ensuring variables are named the same in each dataset.

    Perhaps this can get you started in a useful direction. I assume you have exactly the same number of variables in each dataset, in precisely the same order within each dataset, ignoring the names.

    Code:
    clear *
    cls
    
    frame create A
    frame change A
    input byte(a b c)
    1 2 3
    end
    list
    qui ds *
    local origvars `r(varlist)'
    rename (*) (v#), addnumber
    qui ds *
    local newvars `r(varlist)'
    
    frame create B
    frame change B
    input byte(d e f)
    5 6 7
    end
    list
    rename (*) (v#), addnumber
    
    frame copy A Want
    frame change Want
    frameappend B
    rename (`newvars') (`origvars')
    list
    Result

    Code:
    // frame A
    . list
    
         +-----------+
         | a   b   c |
         |-----------|
      1. | 1   2   3 |
         +-----------+
    
    // frame B
    . list
    
         +-----------+
         | d   e   f |
         |-----------|
      1. | 5   6   7 |
         +-----------+
    
    // A appended to B
    . list
    
         +-----------+
         | a   b   c |
         |-----------|
      1. | 1   2   3 |
      2. | 5   6   7 |
         +-----------+

    Comment


    • #3
      Thank you for the suggestion. Is there a way to do this without using "frame" command?

      Comment


      • #4
        Sure, the same effect can be achieved using -tempfile-s with minor modification to the above code.

        Code:
        clear *
        cls
        
        tempfile a b
        input byte(a b c)
        1 2 3
        end
        list
        qui ds *
        local origvars `r(varlist)'
        rename (*) (v#), addnumber
        save `a'
        qui ds *
        local newvars `r(varlist)'
        
        drop _all
        input byte(d e f)
        5 6 7
        end
        list
        rename (*) (v#), addnumber
        save `b'
        
        drop _all
        append using `a' `b'
        rename (`newvars') (`origvars')
        list

        Comment


        • #5
          This works very well. Thank you!

          Originally posted by Leonardo Guizzetti View Post
          Sure, the same effect can be achieved using -tempfile-s with minor modification to the above code.

          Code:
          clear *
          cls
          
          tempfile a b
          input byte(a b c)
          1 2 3
          end
          list
          qui ds *
          local origvars `r(varlist)'
          rename (*) (v#), addnumber
          save `a'
          qui ds *
          local newvars `r(varlist)'
          
          drop _all
          input byte(d e f)
          5 6 7
          end
          list
          rename (*) (v#), addnumber
          save `b'
          
          drop _all
          append using `a' `b'
          rename (`newvars') (`origvars')
          list

          Comment

          Working...
          X