Thanks for this. I think you should change the -index()-
calls back again. I didn't spell it out but I didn't use -index()-
because of the limit on the length of a string expression.
Similarly, the calls to -lower()- are a little dangerous. It would be safer
to cycle through the words of the argument and feed them to -lower()-
individually. Admittedly, it seems unlikely that the search string
will hit the limit on string expressions.
Other than that, you are running with this program,
so you should feel free to make yourself the first author
and so write the help file....
I like the idea of trying to persuade StataCorp to add this ability
to -lookfor-.
Nick
[email protected]
-----Original Message-----
From: Roger Harbord [mailto:[email protected]]
Sent: 04 August 2004 10:24
To: [email protected]; Nick Cox
Subject: RE: searching the notes
Many thanks Nick for astoundingly quick program writing and
memory of past
threads. I'm afraid i didn't follow that thread on 11 May and
i've found
searching Statalist archives is often pretty hopeless in
practice unless you
can remember something of a thread to narrow things down
(just try searching
for "notes"... 234 results is too much for me esp. as most
of them seem to be
returned inappropriately - e.g. "notes" appears only in
subject of the "Next
by Date" message!).
In testing -searchnotes- i found that parsing with -syntax
name- wasn't ideal
in that it prevented me searching for e.g. "g/L" or "protein
C". Also I'd
prefer it to be case insensitive, like -lookfor- (i just found Dan
Blanchette's -lookforit- on SSC which allows a more flexible
approach but
haven't attempted that). I've stolen some code from the
built-in lookfor.ado
and ended up with the code below. -lookfor- uses -index-
instead of -substr
local- which seems more elegant but i can't take any credit for that
obviously. Unlike -lookfor- the code below only searches for
one string at a
time and does the same thing whether or not that string is in
quotes. No
particular reason other than that's what i usually want
myself and the
behaviour of -lookfor- is a bit more complex to program.
However personally i'd find it more convenient if this was
part of the
official -lookfor- (maybe with a -,notes- option) if
StataCorp are listening
and happen to think it worthwhile..
Roger.
----------------------------------------------------
Roger Harbord mailto:[email protected]
Department of Social Medicine, University of Bristol
*! NJC / RMH 1.0.1 4 Aug 2004
program searchnotes
version 8
if `"`*'"' == "" {
di as err "nothing to search for"
exit 198
}
local what = lower(`"`*'"')
local nnotes "`: char _dta[note0]'"
if "`nnotes'" != "" {
forval i = 1/`nnotes' {
local char : char _dta[note`i']
if index(lower(`"`char'"'),`"`what'"') {
di as res "_dta: " as txt `"`char'"'
}
}
}
foreach v of var * {
local nnotes "`: char `v'[note0]'"
if "`nnotes'" != "" {
forval i = 1/`nnotes' {
local char : char `v'[note`i']
if
index(lower(`"`char'"'),`"`what'"') {
di as res "`v': " as
txt `"`char'"'
}
}
}
}
end
--On 03 August 2004 19:55 +0100 Nick Cox <[email protected]> wrote:
> This would seem approachable
> as a variation on various programs
> posted on 11 May, e.g.
>
> *! NJC 1.0.0 3 Aug 2004
> program searchnotes
> version 8
> syntax name(name=what)
>
> local nnotes "`: char _dta[note0]'"
> if "`nnotes'" != "" {
> forval i = 1/`nnotes' {
> local char : char _dta[note`i']
> local WHAT : ///
> subinstr local char `"`what'"' `"`what'"',
count(local n)
> if `n' {
> di as res "_dta: " as txt `"`char'"'
> }
> }
> }
>
> foreach v of var * {
> local nnotes "`: char `v'[note0]'"
> if "`nnotes'" != "" {
> forval i = 1/`nnotes' {
> local char : char `v'[note`i']
> local WHAT : ///
> subinstr local char `"`what'"' `"`what'"',
count(local n)
> if `n' {
> di as res "`v': " as
txt `"`char'"'
> }
> }
> }
> }
> end
>
> I'd appreciate a more elegant method of counting...
>
> Nick
> [email protected]
>
>> -----Original Message-----
>> From: [email protected]
>> [mailto:[email protected]]On Behalf Of
>> Roger Harbord
>> Sent: 03 August 2004 19:29
>> To: Statalist; [email protected]
>> Subject: st: searching the notes
>>
>>
>> Is there any way of searching all the -notes- for a given
>> string? I find
>> -lookfor- very useful when dealing with datasets with many
>> variables but it
>> would be even more useful if it had a -, notes- option to
>> allow searching of
>> the notes attached to the variables too. Alternatively maybe
>> if -notes list-
>> had a -, find(string)- option..?
>>
>> I realise that you need to have a lot of notes in your
>> dataset to find this
>> useful - we do here as our database manager has set up a
>> clever system (don't
>> ask me how it works) to export from Access to Stata so each
>> Field Description
>> in Access becomes a note attached to that variable in Stata.
>> (The Field
>> Descriptions are too long to make useful variable labels and
>> often exceed the
>> limit of 80 characters for variable labels.)
>>
*
* 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/