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

Re: st: Tabulating "missing" values


From   sdriver@stata.com (Shannon Driver, StataCorp)
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Tabulating "missing" values
Date   Wed, 09 Apr 2003 15:50:51 -0500

Michael Oakes <oakes@epi.umn.edu> wrote:

> Hoping this isn't too obvious, but I cannot figure out how to fill-in rows
> for response values that do not appear in my data but do exist
> hypothetically.

> Imagine a variable, call it q5, that has possible responses 1, 2, 3, and 4;
> let it be a simple Likert scale for example. The trouble is if survey
> respondents only endorse/check values of 3 or 4, then my table will look
> like this

> . tab q5, nolab

>             |
>        q5   |
>             |      Freq.     Percent        Cum.
> ------------+-----------------------------------
>           3 |         14       24.14       24.14
>           4 |         44       75.86      100.00
> ------------+-----------------------------------
>       Total |         58      100.00

> But what I want is (something like) this, which I made the "hard-way" via
> cut/paste

>        q5   |
>             |      Freq.     Percent        Cum.
> ------------+-----------------------------------
>           1 |          0        0.0          0.0
>           2 |          0        0.0          0.0
>           3 |         14       24.14       24.14
>           4 |         44       75.86      100.00
> ------------+-----------------------------------
>       Total |         58      100.00

> In this example the hypothetical values of 1 and 2 are represented, which is
> nice if I want to apply value labels and such for presentation.

> I've looked at -tabcond- and a few other adaptations but cannot find what I
> want -- I do not seem to understand -tabcond- if that would do the trick.

> Solutions?

Though this may not be the most elegant code, here is one way to get the 
results that I believe you want.  I'm sure it could be expanded to allow more
statistics and options.

    program tabrange
        version 8.0
        syntax varname, range(numlist)

        foreach i in `range' {
            local rangelist "`rangelist' `i',"
        }
        local rangelist = substr("`rangelist'",1,length("`rangelist'")-1)

        tempvar freq
        gen `freq' = 1

        qui count if inlist(`varlist',`rangelist')
        if r(N) != 0 {
            preserve
                collapse (sum) `freq' if inlist(`varlist',`rangelist'), ///
                    by(`varlist')
                gen Freq = `freq'

                foreach i in `range' {
                    qui count if `varlist' == `i'
                    if r(N) == 0 {
                        local obs = _N + 1
                        qui {
                            set obs `obs'
                            replace `varlist' = `i' in l
                            replace Freq = 0 in l
                        }
                    }
                }

                sort `varlist'
                qui summ Freq

                gen Percent = Freq/r(sum) * 100
                gen Cum = sum(Percent)

                format %5.2f Percent Cum

                list `varlist' Freq Percent Cum, sum(Freq Percent) ///
                    labvar(`varlist') noobs
            restore
        } 
        else {
            di as txt "No observations for " as res "`varlist'" ///
                as txt " in given range."
        }
    end

Here are the results you get:

    . tabrange q5, range(1/4)

      +-------------------------------+
      |  q5   Freq   Percent      Cum |
      |-------------------------------|
      |   1      0      0.00     0.00 |
      |   2      0      0.00     0.00 |
      |   3     14     24.14    24.14 |
      |   4     44     75.86   100.00 |
      |-------------------------------|
      | Sum     58    100.00          |
      +-------------------------------+

    . tabrange q5, range(1 3 4 8)

      +-------------------------------+
      |  q5   Freq   Percent      Cum |
      |-------------------------------|
      |   1      0      0.00     0.00 |
      |   3     14     24.14    24.14 |
      |   4     44     75.86   100.00 |
      |   8      0      0.00   100.00 |
      |-------------------------------|
      | Sum     58    100.00          |
      +-------------------------------+

    . tabrange q5, range(14)
    No observations for q5 in given range.

--Shannon Driver
  sdriver@stata.com

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