Question

I have a dataset containing different product values generated in each simulation, with the following layout:

+------------+-------+-------+-------+
| simulation |  v1   |  v2   |  v3   |
+------------+-------+-------+-------+
|          1 | 0,500 | 0,400 | 0,300 |
|          2 | 0,900 | 0,800 | 0,800 |
|          3 | 0,100 | 0,200 | 0,300 |
+------------+-------+-------+-------+

The variable names v1, v2, v3 are labelled as product ids and are not displayed at the header of the dataset. I need to reshape this dataset to long format so it would like this:

+------------+----+----------+-------+
| simulation | id |  label   | value |
+------------+----+----------+-------+
|          1 | v1 | 01020304 | 0,500 |
|          1 | v2 | 01020305 | 0,400 |
|          1 | v3 | 01020306 | 0,300 |
|          2 | v1 | 01020304 | 0,900 |
|          2 | v2 | 01020305 | 0,800 |
|          2 | v3 | 01020306 | 0,800 |
|          3 | v1 | 01020304 | 0,100 |
|          3 | v2 | 01020305 | 0,200 |
|          3 | v3 | 01020306 | 0,300 |
+------------+----+----------+-------+

The standard code reshape long v , i(simulation) j(_count) is not applicable in this case, as I need to reshape the variable labels and keep them in the dataset as variable values. Was wondering if there exists a way to make this kind of transposition with variable labels?

Was it helpful?

Solution

Only one idea seems needed here. If your variable labels would otherwise disappear, save them in a local macro before a reshape and then apply them as value labels afterwards. The FAQ cited earlier gives the flavour.

Sandpit to play in:

input simulation   v1     v2     v3   

    simulat~n         v1         v2         v3
1. 1  0.500  0.400  0.300 
2. 2  0.900  0.800  0.800 
3. 3  0.100  0.200  0.300 
4. end 

label var v1 "01020304"
label var v2 "01020305"
label var v3 "01020306"

Sample code:

forval j = 1/3 { 
    local labels `labels' `j' "`: var label v`j''"
} 


reshape long v, i(simulation)  
(note: j = 1 2 3)

Data                               wide   ->   long
-----------------------------------------------------------------------------
Number of obs.                        3   ->       9
Number of variables                   4   ->       3
j variable (3 values)                     ->   _j
xij variables:
                               v1 v2 v3   ->   v
-----------------------------------------------------------------------------

rename v value 
label def label `labels' 
rename _j label
gen id = label 
label val label label  


list 

     +----------------------------------+
     | simula~n      label   value   id |
     |----------------------------------|
  1. |        1   01020304      .5    1 |
  2. |        1   01020305      .4    2 |
  3. |        1   01020306      .3    3 |
  4. |        2   01020304      .9    1 |
  5. |        2   01020305      .8    2 |
     |----------------------------------|
  6. |        2   01020306      .8    3 |
  7. |        3   01020304      .1    1 |
  8. |        3   01020305      .2    2 |
  9. |        3   01020306      .3    3 |
     +----------------------------------+
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top