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]

st: RE: RE: Test position of a whole word within a macro


From   "Brent McSharry (ADHB)" <BrentM@adhb.govt.nz>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: Test position of a whole word within a macro
Date   Wed, 4 Dec 2013 13:33:12 +1300

Thank you Sarah

The problem was in my example - I had unfortunately chosen as an example the last item in the list, and not put the word I was searching for - a silly omission on my behalf. If I redo my example:

local test = *wordposition*("var12 var1 var3", "var1") 
// or equivalent extended macro or mata function

local test should have a value of 2.

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Sarah Edgington
Sent: Wednesday, 4 December 2013 11:54 a.m.
To: statalist@hsphsun2.harvard.edu
Subject: st: RE: Test position of a whole word within a macro

Brent,
Forgive me if I am misunderstanding what you are trying to do but it looks
like from your initial example that you are trying to count the number of
words in string.
If that is in fact what you're trying to do, you might try the function
wordcount.  So you'd have something like:
local test=wordcount("var12 var3 var1")

-Sarah


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Brent McSharry
(ADHB)
Sent: Tuesday, December 03, 2013 2:36 PM
To: statalist@hsphsun2.harvard.edu
Subject: st: Test position of a whole word within a macro

Dear statalisters

I need to determine the position (in words, not characters) a word is within
a macro. For example:

local test = *wordposition*("var12 var3 var1")

local test should have a value of 3.

It will be used within a loop which will be called hundreds of times, so it
must be performat, and for this reason I will only use a foreach loop if
this is the only way.

The programming (ie not stata) solution would be to create a 'dictionary'/
hash table of the string values.

The pseudo-code for what I am trying to do () is . matrix `outmat' =
J(`indepcount', 1,0) . matrix rownames `outmat' = `indepvars'
. forvalues i=1(1)`iterations' {
.	//reassign division of development and testing data sets
.	//build model & exclude unwanted variables
.	local included:colfullnames(e(b)) 
.	//remove _cons from `included'
.	foreach v in `included' {
.		//if vectors could be referred to by string, one would use
.		// `outmat'[`v'] = outmat'[`v'] + 1
.		//I don't think the above is possible in stata, so instead:
.		local i = //which word 
.		`outmat'[`i'] = outmat'[`i'] + 1
.	}
. }

Does anyone have any ideas on a performant way to test for an exact word
match? Thank you

Brent McSharry
Intensivist
Starship Hospital Auckland

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/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/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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