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]

Re: st: How to get a list of the names of characters associated with a variable


From   Eric Booth <[email protected]>
To   [email protected]
Subject   Re: st: How to get a list of the names of characters associated with a variable
Date   Thu, 4 Mar 2010 15:34:41 -0600

>
On Mar 4, 2010, at 2:19 PM, Clyde Schechter wrote:
>   (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.)

Hi Clyde:

I'm not sure whether you're asking to find all the variables that have variable names that contain one of the target characters or string variables that contain your target character in one of the observations? (or possibly you mean characteristics (as Nick suggests)?)


> But I haven't found a way to get a list of all characters associated with
> `v'.  I can display them all with - char list `v'[] -, but that doesn't
> return anything.

To answer your question about returning a character list after the -charlist- command, you want to use the `r(chars)' or `r(sepchars)' macros.


If you want to do something to all variables that have variable names that contain some target character, then you can use the `r(varlist)' created by -lookfor-, so:

****
webuse auto, clear
lookfor k
di "`r(varlist)'"
sum `r(varlist)'
**this will give you the vars "make" and "trunk"
***


If you want to do something to all string variables that have some target character somewhere in it's observations, here's how you could modify your pseudocode (change your target characters in the local macro "targetchars"):

****************
clear

inp str10 var1 str10 var2 str10 mar3
"aaaa" "bbbb" "cccc"
"abab" "cccc" "dddd"
"z-z-" "?/$%" "AAAA"
end

**example:  get a list of characters:
	charlist var1
	di "`r(chars)'"
	di "`r(sepchars)'"

**
ds, has(type string)

**target characters**
local targetchars b t 2 -

foreach t of local targetchars {
	foreach v in `r(varlist)'  {
    	 cap drop flag_`v'
    	 g flag_`v' = index(`v', "`t'")
    	qui su flag_`v'
        	if `r(max)' > 0 {
        		local i  "`i'  `v'"
        	}
           cap drop flag_`v'
    }
}
	di "`i'"
**do something with only these vars:
foreach v of local i {
	di "`v'"
	sum `v'
	*do something with these vars*
	}
****************

~ Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754


On Mar 4, 2010, at 2:19 PM, 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.)
> 
> So my basic idea is:
> 
> foreach v of varlist _all {
>     get a list of all characters associated with `v' in a local char_list
>     foreach c of local char_list {
>          if index("`:char `v'[`c']'", "`target_string'") > 0 {
>               found it -- generate suitable output
>          }
>     }
> }
> 
> But I haven't found a way to get a list of all characters associated with
> `v'.  I can display them all with - char list `v'[] -, but that doesn't
> return anything.
> 
> Anyone have any ideas about this, or an alternative way for me to
> generalize -lookfor-?  Thanks in advance for any help.
> 
> Clyde Schechter, MA MD
> Associate Professor of Family & Social Medicine
> Albert Einstein College of Medicine, Bronx, NY, USA
> 
> Please note new e-mail address: [email protected]
> 
> *
> *   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index