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

st: RE: by and missing values question

From   "Nick Cox" <>
To   <>
Subject   st: RE: by and missing values question
Date   Mon, 11 May 2009 11:00:03 +0100

Question [3] is easiest to answer: excluding missing values is an
automatic effect of following -marksample touse- with commands that work
-if `touse'. 

Question [1] has unhelpful and helpful answers. As your command cycles
over the categories of the varlist fed to -by:- each set of results is
returned, but then immediately afterwards overwritten by the next set.
So all you can see afterwards with -return list- is the last set of
results. This was an important reason for StataCorp to add -statsby-.
There are other solutions too, but involving different programming

Question [2] is a good question. You could engineer your program so that
it always returned total results too, but the effect would inevitably be
that it did that every time round the loop. In practice I'd tend to
issue the command twice, once without and once with -by:-. 

A very small detail: In 

    qui su  `dif' if `touse' [`weight'`exp'] , meanonly

the -quietly- is redundant, as -summarize, meanonly- never produces
output in the results window (at most error messages). It does no harm,


Nikolaos Kanellopoulos

I have written the following very simple program

cap pr drop logdiff
program define logdiff , rclass byable(recall)

    syntax varlist(min=2 max=2) [if] [in] [fweight aweight] 

    version 8.0
    tempvar dif
    marksample touse 
    qui count 
    local N = r(N)

    qui count if `touse'
    if r(N) == 0 error 2000
    local n = r(N)

    qui gen `dif' = (`n'/`N')*abs(ln(`2') - ln(`1'))  if `touse'    
    qui su  `dif' if `touse' [`weight'`exp'] , meanonly
    loc diff = r(mean)
    di as text "Log Difference: " as result `diff'
    return scalar diff = `diff'
    return local N = `N'
    return local n = `n'

My questions are:

[1] How can I return the results for all categories when I use the by
option and not only for the last category?

[2] How can I display and return the results for the total of a category
when I use the by option? i.e. return the same results as without the by

[3] How can I exclude the missing values from my estimations both when I
use and not use the by option?

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index