Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Preserve Variable Labels After Reshape


From   Ryan Kessler <ryan.kessler.stata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Preserve Variable Labels After Reshape
Date   Wed, 19 Dec 2012 22:15:22 -0500

I agree with Nick and Joerg: preserving the variable labels doesn't
make sense, given that the mapping isn't one-to-one. Nevertheless, I
think I understand what you're trying to do. How about something like
this?

*********************************************************************************

clear
input y id x2007 x2008 x2009 z2007 z2008 z2009
18 1 12 16 18 20 21 19
10 2 11 17 17 33 32 19
12 3 10 10 22 19 17 18
end

// Labeling variables
foreach v of varlist x* z* {
	label variable `v' "`=substr("`v'",1,1)' factor
(`=substr("`v'",length("`v'")-3,4)')"
	
}
desc *

// Looping over stems to capture variable labels
foreach s in x z {
	local `s'label = subinstr("`: variable label `s'2007'", " (2007)", "", 1)
}

reshape long x z, i(id) j(year)

// Looping over stems again to reinstate labels
foreach s in x z {
	label variable `s' "``s'label'"
}
desc *

************************************************************************

Ryan

On Wed, Dec 19, 2012 at 10:06 PM, Kieran McCaul
<kieran.mccaul@uwa.edu.au> wrote:
> ...
>
>
> local vname:  variable label campus80
>
> reshape ....
>
> label variable campus "`vname'"
>
>
>
>
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of David Torres
> Sent: Thursday, 20 December 2012 10:46 AM
> To: statalist@hsphsun2.harvard.edu
> Subject: RE: st: Preserve Variable Labels After Reshape
>
> Joerg,
>
> What I mean to preserve is the description of the variable stub, in your example the x.  x1 and x2 could both, for instance, be labeled as, let us say, "the x factor."  When I reshape from wide to long, the description of the variable disappears.  I want to simply keep that description, which I thought was called the variable label.
>
> From my own file:  I have variables called campus08 campus09 campus10.  The description for these variables is the same in all three years (the two-digit number after the variable stub): "Campus ID Where Student Attended School."  When I reshape from wide to long, I now have one variable, campus, that has no description.  How can I keep the label "Campus ID Where Student Attended School"?
>
> Thanks,
>
>
> ----------------------------------------
>> Date: Wed, 19 Dec 2012 21:24:41 -0500
>> Subject: Re: st: Preserve Variable Labels After Reshape
>> From: joerg.luedicke@gmail.com
>> To: statalist@hsphsun2.harvard.edu
>>
>> Say we have 2 observations and 2 variables in wide format which we
>> would like to reshape long.
>>
>> The data in wide format would look like:
>>
>> clear
>> set obs 2
>> gen id=_n
>> forval i=1/2 {
>> gen x`i'=`i'
>> label var x`i' "label `i'"
>> }
>>
>> . li
>>
>> +--------------+
>> | id x1 x2 |
>> |--------------|
>> 1. | 1 1 2 |
>> 2. | 2 1 2 |
>> +--------------+
>>
>> Both variables x1 and x2 are labeled. Now we reshape:
>>
>> reshape long x, i(id) j(year)
>>
>> and get:
>>
>>
>> . li
>>
>> +---------------+
>> | id year x |
>> |---------------|
>> 1. | 1 1 1 |
>> 2. | 1 2 2 |
>> 3. | 2 1 1 |
>> 4. | 2 2 2 |
>> +---------------+
>>
>> Where exactly would you want the original labels to go? What was
>> stored in two variables before is now stored in one variable.
>>
>> Joerg
>>
>>
>> On Wed, Dec 19, 2012 at 9:13 PM, David Torres <writeon4truth2@msn.com> wrote:
>> > The 150 variables I have, however, are the same in each of five files, each representing a different year. The labels are the same. I am simple trying to keep those labels after a reshape. I fail to see how in my case this doesn't make sense.
>> >
>> > Cheers
>> >
>> > Sent from my iPhone
>> >
>> > On Dec 19, 2012, at 8:07 PM, "Joerg Luedicke" <joerg.luedicke@gmail.com> wrote:
>> >
>> >> I agree with Nick that this makes no sense. Say you reshape your 150
>> >> variables such that you have only one variable in long format, then
>> >> you have only one variable for which you can use one label.
>> >>
>> >> Joerg
>> >>
>> >> On Wed, Dec 19, 2012 at 8:58 PM, David Torres <writeon4truth2@msn.com> wrote:
>> >>> Okay, is there a way to save the labels and reinsert them after reshape? I'm not too keen on having to retype labels for 150 variables.
>> >>>
>> >>> Thanks
>> >>>
>> >>> Sent from my iPhone
>> >>>
>> >>> On Dec 19, 2012, at 7:24 PM, "Nick Cox" <njcoxstata@gmail.com> wrote:
>> >>>
>> >>>> How could they be preserved? A -reshape- is not a one-to-one mapping.
>> >>>>
>> >>>> Nick
>> >>>>
>> >>>> On Thu, Dec 20, 2012 at 1:16 AM, David Torres <writeon4truth2@msn.com> wrote:
>> >>>>> I'm reshaping some data from wide to long and would like to preserve my variable labels. Does anyone know an easy way to do this?
>> >>>>>
>> >>>>> I've already tried the user-written commands -longshape-, -reshape7-, and -reshape8-. My variable labels were not preserved.
>> >>>> *
>> >>>> * For searches and help try:
>> >>>> * http://www.stata.com/help.cgi?search
>> >>>> * http://www.stata.com/support/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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