Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: UPDATE: once again -reshape- and labels


From   Andrea Bennett <mac.stata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: UPDATE: once again -reshape- and labels
Date   Tue, 17 Jun 2008 08:29:26 +0200

Thank you so much! Your app works (as far as I see) perfect. You might want to make it available online as I think it will help quite some people out there. I hope I did the install correct. I just saved the code below in a text file and stored it in the ado folder of Stata, with .ado as the file type.

Thank you again and kind regards,

Andrea


On Jun 17, 2008, at 1:51 AM, Roy Wada wrote:



I'm sorry but your suggestion does not work. As - hopefully
clear to all - I have described, it is about temporarily saving
the label for e.g. Ax1 and then - after reshape long - apply the
label of Ax1 to Ax. This should be automated for all variables I
want to reshape so that I won't have to change the labels manually.

Andrea
This question comes up every year. Here's a program I use.
It will grab a label from the earliest year. The line about
-cap local v: word 1 of `names'- can be adjusted or cycled
through, but that would be overkill.

Roy

*! reshapeL 0.9 Jun2008 by roywada@hotmail.com
program define reshapeL
version 7

gettoken part1 rest1: 0, parse(",")
gettoken part2 rest2: part1, parse(" ")

foreach v in `rest2' {
cap unab names : `v'*
cap local v: word 1 of `names'

local count 0
local stop 0
while `count'>-50 & `stop'==0 {
local count=`count'-1
cap local last=trim(substr("`v'",`count',.))
capture confirm number `last'
if _rc!=0 {
local stop 1
}
}

cap local shortname=trim(substr("`v'",1,length("`v'")`=`count'+1'))
cap local l`shortname' : variable label `v'
di `"`v'"' _col(20) `"`l`shortname''"'
}
reshape `0'

foreach v in `rest2' {
cap label var `v' "`l`v''"
}

end


* create example data
webuse grunfeld,clear
reshape wide invest mvalue kstock time, i(company) j(year)
label var invest1935 "yum yum"

* preserves your labels as you go from wide to long
reshapeL long invest mvalue kstock time, i(company) j(year)



_________________________________________________________________
It’s easy to add contacts from Facebook and other social sites through Windows Live™ Messenger. Learn how.
https://www.invite2messenger.net/im/?source=TXT_EML_WLH_LearnHow
*
* 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/

*
*   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