Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Data file restructure


From   "Scott Merryman" <scott.merryman@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Data file restructure
Date   Mon, 8 Dec 2008 10:26:23 -0600

Something like this?

clear
input id m81 m82 m85 a81 a82 a85
1 1.5 1.75 2   16 17 20
2 2.3 2.5  2.6 17 18 21
3 1.8 2    2.3 24 25 28
end
reshape long m a, i(id) j(year)
l
drop year
rename m y
reshape wide  y , i(id ) j(a)
l

Scott


On Mon, Dec 8, 2008 at 7:25 AM, Michael Beets <beetsstatalist@gmail.com> wrote:
> Data transformation
>
>
>
> I have a wide formatted data set (each row is an individual measured
> over time) containing 15 measurements beginning at 1981 and ending at
> 2006 (example: m81, m82, m85, m86… m2006). At the initial year of
> measure (1981) the age range of individuals spanned 16 to 24yrs-old.
> The age of each individual at each measurement occasion is in 15 age
> variables (example: a81, a82, a85, a86… a2006).
>
>
>
> What I want to do is restructure the dataset so age (in years) is the
> variable with the measurement value listed for each individual.
>
>
>
> Here is an example of what I think this would look like:
>
>
>
> Current dataset:
>
> Id          m81          m82          m85          a81          a82          a85
>
> 1           1.5           1.75           2                16
>  17            20
>
> 2          2.3           2.5            2.6               17
> 18            21
>
> 3          1.8           2              2.3                24
>  25            28
>
>
>
>
>
> Restructured dataset
>
> Id            y16         y17         y18         ….           Y20
>    y21         y22         y23         y24         y25         …
>       y28
>
> 1              1.5          1.75                                        2
>
> 2                            2.3          2.5
>                2.6
>
> 3
>                                                         1.8
> 2                              2.3
>
>
>
> I've attempted to use "reshape" but this does not seem to give me the
> data structure I am after. I also attempted to write some code using
> "gen y16 = m81 if a81==16", but this cumbersome and subject to error.
> Is there an easier set of commands to restructure the data as
> indicated above?
>
> Thanks.
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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