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]
Re: st: How to get a list of the names of characters associated with a variable
From
Jeph Herrin <[email protected]>
To
[email protected]
Subject
Re: st: How to get a list of the names of characters associated with a variable
Date
Thu, 04 Mar 2010 18:01:28 -0500
Clyde Schechter wrote:
I'm trying to write an ado file that will generalize the -lookfor- command
so that it also searches for the target text in characters associated with
variables. (I need this because I have a data set with a large number of
variables, and the key information is often found not in the label [which
was truncated at 80 characters], but in a character--and the characters
are not consistently named.)
Below is a program I wrote which does what I think you want. If
you specify a value for the characteristic, it finds only those
variables, if you don't specify a value, it finds all variables
with the characteristic defined. Note that case is ignored.
Example
dchar t*, char(type likert)
finds all variables starting with -t- that have characteristic
-type- set equal to -likert-
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 : subinstr local char "`char1'" ""
local char2=lower(trim("`char2'"))
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']
local charval=lower("`charval'")
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/