Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Reshape question


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Reshape question
Date   Thu, 17 Apr 2003 08:55:52 +0100

Alejandro Riaņo

> > I have the following monthly dataset:
> >
> > date        Argentina     Brazil  Colombia .....
> > 01/01/90       x1             y1       z1
> > 02/01/90       x2             y2       z2
> >    .           .              .        .
> >    .           .              .        .
> >
> > and so on..
> >
> > where Xi, Yi and Zi are values for a given time-series
> >
> > I'd like this change this dataset into a long format,
> > like this:
> >
> >
> > date         Country       Serie
> > 01/01/90      ARG           x1
> > 02/01/90      ARG           x2
> > 01/01/90      BRA           y1
> > 02/01/90      BRA           y2
> > 01/01/90      COL           z1
> > 02/01/90      COL           z2

Radu Ban

> first you have to rename your countries so that you have a
> common stub
> "serie"
>
> for example you could do (i just learned to this recently on
> Statalist, namely from Nick Cox):
>
> local fullnames "Argentina Brazil Colombia..."
> local codes "ARG BRA COL..."
>
> local i = 1
> foreach x of local fullnames {
> 	local y : word `i' of `codes'
> 	rename `x' serie`y'
> 	local i = `i' + 1
> }
>
> *now move on to actual reshaping
>
> reshape long serie, i(date) j(country) string
>
> *note that you have to add the [string] option at the end
> *since the suffix is in string format
>
> hope this works, i didnt have time to test it.

As Radu says, the key is getting a common stub. This
is emphasised rather more strongly at
http://www.stata.com/support/faqs/data/reshape3.html
than at [R] reshape.

If the codes "ARG" "BRA" etc. were not crucial,
another way to do it would be something like:

foreach v of var Argentina-Zimbabwe {
	rename `v' Serie`v'
}
reshape long Serie, i(date) j(country) string

Nick
n.j.cox@durham.ac.uk

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index