I have a long dataset that has a wide variable in it (loc). Loc represents location of work per year.
I want to reformat the 'loc' variable to look like this:
id | year | loc | |
1 | 2010 | london | |
1 | 2011 | london | |
1 | 2012 | leeds | |
1 | 2013 | leeds | |
2 | 2013 | birmingham |
I can't seem to reshape it in the way I would like. Very grateful for your help with this.
Molly
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(id year) str10 loc1 str6(loc2 loc3) str5 loc4 str4 loc5 str3 loc6 long(loc11 loc12 loc13 loc14 loc15 loc16) float letb
1 2010 "london" "london" "leeds" "leeds" "." "." 4 3 2 3 1 1 0
1 2011 "london" "london" "leeds" "leeds" "." "." 4 3 2 3 1 1 0
1 2012 "london" "london" "leeds" "leeds" "." "." 4 3 2 3 1 1 0
1 2013 "london" "london" "leeds" "leeds" "." "." 4 3 2 3 1 1 0
2 2013 "birmingham" "london" "london" "bath" "." "." 2 3 4 2 1 1 0
2 2014 "birmingham" "london" "london" "bath" "." "." 2 3 4 2 1 1 0
2 2015 "birmingham" "london" "london" "bath" "." "." 2 3 4 2 1 1 0
2 2016 "birmingham" "london" "london" "bath" "." "." 2 3 4 2 1 1 0
3 2017 "bath" "bath" "." "." "." "." 1 1 1 1 1 1 1
3 2018 "bath" "bath" "." "." "." "." 1 1 1 1 1 1 1
4 2010 "leeds" "leeds" "liv" "liv" "bath" "liv" 3 2 3 4 2 2 0
4 2011 "leeds" "leeds" "liv" "liv" "bath" "liv" 3 2 3 4 2 2 0
4 2012 "leeds" "leeds" "liv" "liv" "bath" "liv" 3 2 3 4 2 2 0
4 2013 "leeds" "leeds" "liv" "liv" "bath" "liv" 3 2 3 4 2 2 0
4 2014 "leeds" "leeds" "liv" "liv" "bath" "liv" 3 2 3 4 2 2 0
4 2015 "leeds" "leeds" "liv" "liv" "bath" "liv" 3 2 3 4 2 2 0
end
label values loc11 loc11
label def loc11 1 "bath", modify
label def loc11 2 "birmingham", modify
label def loc11 3 "leeds", modify
label def loc11 4 "london", modify
label values loc12 loc12
label def loc12 1 "bath", modify
label def loc12 2 "leeds", modify
label def loc12 3 "london", modify
label values loc13 loc13
label def loc13 1 ".", modify
label def loc13 2 "leeds", modify
label def loc13 3 "liv", modify
label def loc13 4 "london", modify
label values loc14 loc14
label def loc14 1 ".", modify
label def loc14 2 "bath", modify
label def loc14 3 "leeds", modify
label def loc14 4 "liv", modify
label values loc15 loc15
label def loc15 1 ".", modify
label def loc15 2 "bath", modify
label values loc16 loc16
label def loc16 1 ".", modify
label def loc16 2 "liv", modify
[/CODE]
0 Response to reshape wide variable to long within long dataset
Post a Comment