splitting rows in stata

I am very new to stata and I am struggling how to split a row into multiple rows

I have the following structure in my table:

id v0pd20 v1pd20 v2pd20 v3pd20 v4pd20 v5pd20 v6pd20 v7pd20

How do I create a new table with the following structure based on the above

id v0pd20 v1pd20 v2pd20 v3pd20
id v4pd20 v5pd20 v6pd20 v7pd20

Any help is greatly appreciated!

Hey Mette,

When you say "table," you just mean your Data Editor, right?

If so, I assume what you are trying to do is create two variables, id1 and id2, with your specified values. With only 8 entries, I suppose the easiest way is to change the Data Editor to "Edit" mode by typing "edit" in your Command Window. Then you can manually change the cells pretty effortlessly. More involved, you could try this series of commands:

rename id id1
generate id2 = .
tostring id2, replace
replace id2 = "v4pd20" in 1
replace id2 = "v5pd20" in 2
replace id2 = "v6pd20" in 3
replace id2 = "v7pd20" in 4
drop in 5/8

If you have a larger data set, then simply editing the data you import is probably ideal. Let me know if that's the case. Hope this helps.


Here is another option:
keep id v4pd20 v5pd20 v6pd20 v7pd20
tempfile secondhalf
save "`secondhalf'"
keep id v0pd20 v1pd20 v2pd20 v3pd20
append using "`secondhalf'"
sort id