Statalist


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

Re: st: Reshape-like procedure - help!


From   "K Jensen" <[email protected]>
To   [email protected]
Subject   Re: st: Reshape-like procedure - help!
Date   Tue, 13 Jan 2009 21:28:14 +0000

I was using Stata to prepare data for a prewritten program--not
mine--in another package (MS Access) that expects the data in this
format. I suspected--correctly!--that it would be easier to do this in
Stata than in Access. Access would require either a silly number of
UNION queries or a VBA programming loop that would take many more
lines.

I am still lost in admiration for the neatness of Ulrich's solution.

Karin

2009/1/13 Nick Cox <[email protected]>:
> 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