Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: RE: Triming labels.


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: RE: Triming labels.
Date   Fri, 12 Nov 2004 18:52:21 -0000

Trying to reverse engineer the real problem 
from this, it would seem that you want to 
hack at the labels that -tabstat- is using
when a categorical variable that is 
a numeric variable with value labels is 
applied. 

Certainly you will get problems with your code 
when blanks are present in value labels. One 
solution is to use nested quotation marks 
to make sure that a variable label remains 
bound together. 

However, the whole beast is a downstream 
solution when an upstream solution is 
probably easier. 

-decode- your numeric variable to string, 
hack at its values with -subinstr()- 
and then issue -tabstat-. 

Here is a silly example to make the point. 

. sysuse auto 

. label def foreign 0 "Domestic cars" 1 "Foreign cars" , modify

. label val foreign foreign

. decode foreign, gen(foreign2) 

. tabstat mpg, by(foreign2) 

Summary for variables: mpg
     by categories of: foreign2 (Car type)

     foreign2 |      mean
--------------+----------
Domestic cars |  19.82692
 Foreign cars |  24.77273
--------------+----------
        Total |   21.2973
-------------------------

. replace foreign2 = subinstr(foreign2," ","",.) 
(74 real changes made)

. tabstat mpg, by(foreign2) 

Summary for variables: mpg
     by categories of: foreign2 (Car type)

     foreign2 |      mean
--------------+----------
 Domesticcars |  19.82692
  Foreigncars |  24.77273
--------------+----------
        Total |   21.2973
-------------------------

Nick 
[email protected] 

[email protected]
 
> I mean:
>               mat M= M \r(Stat`j')
> Thanks David.
> 
> This solution is to label a variable. What if you want to 
> save as a macro?
> I try your suggestion but it returns missing.
> 
> In fact, I want to put the label in a local macro. I am doing 
> something like this:

> prog mytab
> 
> ...
> 
> qui tabstat `varlist' [`weight' `exp'], stats(mean) long save
> mat M=r(StatTot)
> local matvar "National"
> 
> foreach i in urban region sex marstat {
>        cap confirm numeric `i'
>        qui {
>                lab li `i'
>                local min = r(min)   /*To get the minimum value*/
>                local max=r(max) /*To get the maximum value*/
>         }
> 
> qui tabstat `varlist' [`weight' `exp'], stats(mean) by(`i') long save
>       forval j=`min'/`max' {
>                local lab`j': label `i' `j'
>                local matvar "`matvar' `lab`j''"
>               mat M= M \r(Stat`i')
>         }
>     }
> }
> mat M = M'
> local matvar: list uniq matvar
> mat colnames M = `matvar'
> mat li M, f(%4.3f)
> end
> 
> I am getting conformability error because the labels have blanks.

David Harrison
 
> -trim()- only removes leading and trailing blanks. To remove 
> embedded blanks you
> will need to use something like -subinstr()-.
> 
> Are you talking about variable labels or value labels? If 
> variable labels, then
> something along the lines of
> 
> foreach v of varlist _all {
>              label var `v' "`=subinstr("`: var lab `v''"," ","",.)'"
> }
> 
> should remove all embedded blanks from all variable labels 
> (which may be a
> little overkill!) - to limit to certain variables change the 
> -_all- to the
> varlist you want changed.
> 
> Something along similar lines ought to work for value labels.

[email protected] [mailto:[email protected]]
 
> How to remove blanks in labels (for example : "Region\ South" to
> "Region\South")?
> I am trying trim but it is not working.

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