Q1) I have a following wide data and want to convert into long type as follows:
id | year | quantity | id0 | year0 | quantity0 | id1 | year1 | quantity1 |
1 | 1985 | 100 | 1 | 1986 | 150 | 1 | 1987 | 50 |
2 | 1985 | 200 | 2 | 1986 | 400 | 2 | 1987 | 100 |
3 | 1985 | 300 | 3 | 1986 | 500 | 3 | 1987 | 180 |
...
id | year | quantity |
1 | 1985 | 100 |
1 | 1986 | 150 |
1 | 1987 | 50 |
2 | 1985 | 200 |
2 | 1986 | 400 |
2 | 1987 | 100 |
3 | 1985 | 300 |
3 | 1986 | 500 |
3 | 1987 | 180 |
Tricky part for me is that variables are labelled var, var0, var1...var30, which making it difficult to use loops.
So in this case, I don't think simply the following code would not work:
Code:
reshape long quantity, i(id) j(year)
Or is there any better way to do the job?
Q2) If I have multiple (let's say 10) long type data files as above, is it possible to merge all of files with a single line of merge code? Or should I pair and merge them 10 times?
Sorry for bombarding the questions.
Thanks for your help.
0 Response to How to reshape data from wide to long with "tricky" variable names and how to merge multiple data files?
Post a Comment