Statalist


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

RE: st: Reshape-like procedure - help!


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Reshape-like procedure - help!
Date   Tue, 13 Jan 2009 18:17:56 -0000

Ulrich gave a good answer to your question. 

Could you please explain why this data structure is any sense better for
analysis? 

Nick 
[email protected] 

K Jensen

2009/1/13 Ulrich Kohler <[email protected]>:
> Am Dienstag, den 13.01.2009, 16:49 +0100 schrieb Ulrich Kohler:
>> Am Dienstag, den 13.01.2009, 15:37 +0000 schrieb K Jensen:
>> > Hi
>> >
>> > I would like to move from the following format
>> > ID  Age     Weight  Height
>> > 1   23      140     62
>> > 2   64      96      72
>> > 3   49      123     67
>> >
>> > To this format:
>> > ID  Var_name        Var_value
>> > 1   Age     23
>> > 1   Weight  140
>> > 1   Height  62
>> > 2   Age     64
>> > 2   Weight  96
>> > 2   Height  72
>> > 3   Age     49
>> > 3   Weight  123
>> > 3   Height  67
>> >
>> > How can I do this in Stata? None of reshape, stack or xpose are
>> > exactly what I need.
>> >
>> > In reality I have over 100 variables rather than three--so I would
>> > like to avoid having to list the variables by name if possible.
>>
>>
>> local i 1
>> foreach var of varlist Age-Height {
>>       local lab`i' `var'
>>       ren `var' var`i++'
>> }
>> reshape long var, i(ID) j(Var_name)
>> lab val Var_name names
>> forv j = 1/`--i' {
>>       lab def names `j' "`lab`j''", modify
>> }
>
>
> Or even shorter by using a String-Variable for Var_name:
>
> foreach var of varlist Age-Height {
>        ren `var' var`var'
> }
> reshape long var, i(ID) j(Var_name) string

That's _so_ neat--thankyou Ulrich! Exactly what I needed.


*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index