Note that the official Stata command -ds- already has options -has()-
and -not()- that permit identification of variables with or without
named characteristics (but not their contents, as here).
There is much scope for different programming styles, but flagging that
a variable has a particular property can be done by assigning any
non-empty text. Putting informative detail _inside_ characteristics has
disadvantages as well as advantages, as the programmer is
correspondingly obliged to, or able to, look inside.
Nick
[email protected]
Jeph Herrin
[email protected] wrote:
Hi Statalisters,
the following is a "dirty" but - at least for me - useful trick:
Several have suggested using characteristics to store this kind of
info, and I concur. The main disadvantage of both schemes
is that it can be difficult to figure out which variables have
which characteristics - I wrote the program below to list variables
that either have a certain characteristic defined, or have a
certain value for a given characteristic. Ie,
dchar, char(graph percent)
will list all variables with characteristic 'graph' defined
and equal to 'percent'; or
dchar, char(graph)
will simply list all variables with characteristics `graph'
defined. The program also takes a varlist, to limit the search,
and stores the hit list in r(varlist).
hth,
Jeph
*****************************************
* DCHAR
* program to get list of variables with
* certain characteristics
*!
*! version 1.0 27 Aug 2009
*****************************************
program define dchar, rclass
syntax [varlist] , CHar(string)
local char1 : word 1 of `char'
local char2 : word 2 of `char'
local rlist ""
local numvars 0
di ""
foreach V of varlist `varlist' {
local charlist : char `V'[]
local found : list char1 in charlist
if `found'>0 {
if "`char2'"!="" {
local charval : char `V'[`char1']
if "`charval'"=="`char2'" {
di in y "`V'" _col(40) in g ///
"`char1'" _col(50) "`char2'"
local rlist "`rlist'`V' "
local numvars = `numvars'+1
}
}
else {
local charval : char `V'[`char1']
di in y "`V'" _col(40) in g ///
"`char1'" _col(50) "`charval'"
local rlist "`rlist'`V' "
local numvars = `numvars'+1
}
}
}
return local varlist "`rlist'"
return scalar numvars= `numvars'
end
*****************************************
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/