Announcement

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

  • Automating the process of copying and pasting in STATA

    Hi,

    I am trying to copy and paste the first 20 observation onto a new column but the values start at the 10th row and not the first row. I tried manually copying and pasting it by clicking and selecting the observations and finally pasting them to the selected cells in the new column but it seems rather time consuming, especially since I am doing it for multiple columns at a single time. Here is an example of my dataset:

    Click image for larger version

Name:	columns.PNG
Views:	1
Size:	23.4 KB
ID:	1587133


    As you can see, I want the data from the first 20 rows in the column "mergeddata" to be inputted into the second column "mergeddate_01" but the data has to start at the 10th row instead of the first row. What I did so far was to select the first 20 observations by clicking the cells and pasting it to the new column at the 10th row but this process is time-consuming if there are many columns to be created. For the third column, I intend to have observations from the 40th observation in the first column onwards but the data to start at the 21st row instead of the first row. Is there a STATA command to automate the process? I am using the STATA version 16 and any help would be appreciated. Thanks!



  • #2
    Code:
    gen new_variable2 = mergeddata[_n-9] in 1/29
    gen new_variable3 = mergeddata[_n+19] if _n >= 21


    You shouldn't be doing operations like this with cut and paste anyway, unless you are just playing around with your data for fun or exploration. If this is part of data management for a serious purpose, you have to have a complete audit trail of every step, and cut/paste doesn't do that. Do it with code, as shown above. And, of course, as always, make sure you keep a log.

    Comment

    Working...
    X