Statalist The Stata Listserver


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

st: sts graph legend, reshape and value labels


From   "Emma Slaymaker" <Emma.Slaymaker@lshtm.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: sts graph legend, reshape and value labels
Date   Thu, 31 May 2007 15:36:33 +0100

Dear Statalist,
I am often mildly irritated with the legend on sts graph because legend
entries are automatically generated as varname=value label which 
produces something unsiutable for presentation with long variable names
or value labels.   I figure the problem with sts graph stems from
reshape and the fact that you lose value labels when you reshape wide.

I'd like to know if anyone else finds this behaviour a nuisance.  

Admittedly it's simple enough to override sts graph and specify my own
legend but I'm trying to write some extensible code that will be used
for teaching.  The number of categories in the by() variable will vary
and I don't want to waste time in the class writing legends.  For
example:

sysuse cancer
stset studytime,fail(died)
label define drug 1 "useless" 2 "ok" 3 "good"
label values drug drug
sts graph,by(drug)


I looked and couldn't find anything that does exactly what I want
(label variables with the old j var value labels when you reshape wide
and generate a value label from the old j var variables labels when you
reshape long) so I wrote something that solves the reshape problem but
that will be too complicated to explain to my workshop group.  

Could reshape have this option added to it, and sts graph pass this
option onto reshape so that value labels are retained for the by()
variables?  If so, am I the only person who would like this?!  I'm not
teaching until September so a new option would save me having to explain
it at all!

Below is my solution, which does what I want in a clunky way.

Cheers,

Emma

sysuse cancer,clear
stset studytime,fail(died)
label define drug 1 "useless" 2 "ok" 3 "good"
label values drug drug
sts graph,by(drug) name(sts,replace)
sts generate res=s,by(drug)
label var res ""
gen id=_n

**my reshape fix
cap pr drop reshapelabelwide
version 9.2
program define reshapelabelwide
syntax varlist ,i(varlist) j(varname) [KEEPLabels | RENAME]

if "`keeplabels'"=="" & "`rename'"=="" {
reshape wide `varlist',i(`i') j(`j')
}

if "`keeplabels'"~=""{
levels `j',local(jlist)
foreach jval in `jlist' {
local lname`jval':label (`j') `jval'
}

foreach var in `varlist'{
local vlname`var':var label `var'
}

reshape wide `varlist',i(`i') j(`j')

foreach var in `varlist'{
foreach jval in `jlist' {
local newvarlab=`"`vlname`var''"' + " " + `"`lname`jval''"'
label var `var'`jval' "`newvarlab'"
*close j loop
}
*close var loop
}
*close keeplabels if
}



if "`rename'"~=""{
levels `j',local(jlist)
foreach jval in `jlist' {
local lname`jval':label (`j') `jval'
local lname`jval':subinstr local lname`jval' " " "_",all
local lname`jval'=lower("`lname`jval''")
}

foreach var in `varlist'{
local vlname`var':var label `var'
local vlname`var':subinstr local vlname`var' " " "_",all
local vlname`var'=lower("`vlname`var''")
}

reshape wide `varlist',i(`i') j(`j')

foreach var in `varlist'{
foreach jval in `jlist' {
local newvarname=`"`vlname`var''"' + "_" + `"`lname`jval''"'
rename `var'`jval' `newvarname'
*close j loop
}
*close var loop
}
*close rename if
}




end

***

reshapelabelwide res,i( id) j( drug) keeplabel
line res* studytime,connect(J...) sort


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