Announcement

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

  • Copy a variable from one Dataset to another one

    Dear all,
    my goal is to copy two variables from Dataset nr 1 and paste them in Dataset nr 2. Can anyone explain how to do that? I don't need to merge the two Datasets.
    Waiting for your response

  • #2
    I think merge is the only option for you.

    I will keep the 2 variables from your dataset nr 1 with the id number
    and after merge

    Code:
    use dataset1
    keep id var1 var2
    save dataset_merge
    Code:
    use dataset2
    merge 1:1 id using dataset_merge

    Comment


    • #3
      One way to do this would be to save the variables in a matrix, then convert the matrix back to variables in the other.

      i.e.:

      use firstdataset.dta
      svmat var1 var2, matrix(m) rownames(varname)

      use secondataset.dta
      mkmat m, names(col)


      This of course assumes that the data is ordered correctly in both datasets, so that a copy and paste is all you want.

      Comment


      • #4
        Originally posted by David Poensgen View Post
        One way to do this would be to save the variables in a matrix, then convert the matrix back to variables in the other.

        i.e.:

        use firstdataset.dta
        svmat var1 var2, matrix(m) rownames(varname)

        use secondataset.dta
        mkmat m, names(col)


        This of course assumes that the data is ordered correctly in both datasets, so that a copy and paste is all you want.
        Sorry, I messed up; correctly it needs to read

        use firstdataset.dta
        mkmat var1 var2, matrix(m)

        use secondataset.dta
        svmat m, names(col)

        Comment

        Working...
        X