miércoles, 19 de agosto de 2015

First steps in Stata 4: Reshape

Now, one useful command whose use is almost unavoidable, “reshape”.

We were working with the data of Spanish inward foreign direct investment (First steps in Stata 1). You can download by clicking here. The original state of the database is the following:

desc
obs:       250                                             
vars:      23                                          
size:       54,000                                                  
                storage display  value
variable name   type       format  label      variable label
Country                str40      %40s                      Country
fdiflw1993           double  %10.0g                  fdiflw1993
fdiflw1994           double  %10.0g                  fdiflw1994
fdiflw1995           double  %10.0g                  fdiflw1995
fdiflw1996           double  %10.0g                  fdiflw1996
fdiflw1997           double  %10.0g                  fdiflw1997
fdiflw1998           double  %10.0g                  fdiflw1998
fdiflw1999           double  %10.0g                  fdiflw1999
fdiflw2000           double  %10.0g                  fdiflw2000
fdiflw2001           double  %10.0g                  fdiflw2001
fdiflw2002           double  %10.0g                  fdiflw2002
fdiflw2003           double  %10.0g                  fdiflw2003
fdiflw2004           double  %10.0g                  fdiflw2004
fdiflw2005           double  %10.0g                  fdiflw2005
fdiflw2006           double  %10.0g                  fdiflw2006
fdiflw2007           double  %10.0g                  fdiflw2007
fdiflw2008           double  %10.0g                  fdiflw2008
fdiflw2009           double  %10.0g                  fdiflw2009
fdiflw2010           double  %10.0g                  fdiflw2010
fdiflw2011           double  %10.0g                  fdiflw2011
fdiflw2012           double  %10.0g                  fdiflw2012
fdiflw2013           double  %10.0g                  fdiflw2013
fdiflw2014           double  %10.0g                  fdiflw2014
                                                                               
As it can be gathered, we have the amount of investment separately for each year, which can be useful for some type of regressions but not for others. The objective is to convert the 22 FDI variables into two, one that specifies the year of investment and the other that contains the flows of investment. In this case we use “reshape long”. The command is the following:

reshape “variable name without number”, i(unique identifier of each subject) j(period of time)


reshape long fdiflw, i(Country) j(year)


We will notice that now our database only has three variables (Country, year and fdiflw), by using the command describe (desc) you will get the following:


Then, if we want to go back to the original format of the data we do “reshape wide”    

reshape wide fdiflw, i(Country) j(year)  


No hay comentarios:

Publicar un comentario