Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

st: tabulation showing numeric values _and_ value labels [was: ... Features for Stata 14]


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: tabulation showing numeric values _and_ value labels [was: ... Features for Stata 14]
Date   Fri, 14 Feb 2014 11:55:48 +0000

A quick hack produced -tabnumlab- which temporarily prefixes numeric
values to value labels for -tabulate- with either one or two
variables.

This is more direct than -tabnl- posted in 2007 (no user-written
commands need to be installed). The -keeplabels- option lets changes
to the value labels stick.

. sysuse auto, clear
(1978 Automobile Data)

. tabnumlab foreign

   Car type |      Freq.     Percent        Cum.
------------+-----------------------------------
0. Domestic |         52       70.27       70.27
 1. Foreign |         22       29.73      100.00
------------+-----------------------------------
      Total |         74      100.00

. tabnumlab rep78 foreign

    Repair |
    Record |       Car type
      1978 | 0. Domest  1. Foreig |     Total
-----------+----------------------+----------
         1 |         2          0 |         2
         2 |         8          0 |         8
         3 |        27          3 |        30
         4 |         9          9 |        18
         5 |         2          9 |        11
-----------+----------------------+----------
     Total |        48         21 |        69


. tabnumlab foreign rep78 [aw=price]

            |                   Repair Record 1978
   Car type |         1          2          3          4          5 |     Total
------------+-------------------------------------------------------+----------
0. Domestic | 1.4853458  7.7677615   29.02534  8.6126953  1.3681973 |48.2593397
 1. Foreign |         0          0  2.3569635  9.1689882 9.21470865 |20.7406603
------------+-------------------------------------------------------+----------
      Total | 1.4853458  7.7677615  31.382303  17.781684  10.582906 |        69


. tabnumlab rep78 foreign, keeplabels

    Repair |
    Record |       Car type
      1978 | 0. Domest  1. Foreig |     Total
-----------+----------------------+----------
         1 |         2          0 |         2
         2 |         8          0 |         8
         3 |        27          3 |        30
         4 |         9          9 |        18
         5 |         2          9 |        11
-----------+----------------------+----------
     Total |        48         21 |        69

. tab rep78 foreign

    Repair |
    Record |       Car type
      1978 | 0. Domest  1. Foreig |     Total
-----------+----------------------+----------
         1 |         2          0 |         2
         2 |         8          0 |         8
         3 |        27          3 |        30
         4 |         9          9 |        18
         5 |         2          9 |        11
-----------+----------------------+----------
     Total |        48         21 |        69

*! 1.0.0 NJC 14 Feb 2014
program tabnumlab
version 9
syntax varlist(min=1 max=2) [if] [in] ///
[fweight aweight iweight] [, KEEPlabels *]
tokenize "`varlist'"
args row col
local rlbl : value label `row'
if "`rlbl'" != "" {
quietly numlabel `rlbl', add
}
if "`col'" != "" {
local clbl : value label `col'
if "`clbl'" != "" {
quietly numlabel `clbl', add
}
}
tab `row' `col' `if' `in' [`weight' `exp'] , `options'
if "`keeplabels'" == "" {
capture numlabel `rlbl', remove
capture numlabel `clbl', remove
}
end


Nick
[email protected]


Bert Lloyd

That is a nice tip, thanks.

If Statacorp is listening, note that -fre- only does oneway tables.

Alan Neustadtl

> The user written -fre- by Ben Jann available from RePEc (-findit fre-)
> does exactly that.

Bert Lloyd

>> Add a "both" option to -tab- that displays both numeric values and
>> value labels, i.e. similar to -tabnl- from Nick Cox found here:
>>
>> http://www.stata.com/statalist/archive/2007-03/msg00601.html
>>
>> (There may be a more recent or authoritative version of -tabnl-
>> elsewhere, apologies if so. -findit tabnl- did not return anything.)
*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index