Stata: Reshaping dataset – bringing variable labels to variable values -


i have dataset containing different product values generated in each simulation, 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 labelled product ids , not displayed @ header of dataset. need reshape dataset long format 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) not applicable in case, need reshape variable labels , keep them in dataset variable values. wondering if there exists way make kind of transposition variable labels?

only 1 idea seems needed here. if variable labels otherwise disappear, save them in local macro before reshape , apply them value labels afterwards. faq cited earlier gives flavour.

sandpit 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 |      +----------------------------------+ 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -