Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

From   "Sarah Edgington" <>
To   <>
Subject   st: RE: Test position of a whole word within a macro
Date   Tue, 3 Dec 2013 14:53:43 -0800

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")


-----Original Message-----
[] On Behalf Of Brent McSharry
Sent: Tuesday, December 03, 2013 2:36 PM
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
Starship Hospital Auckland

*   For searches and help try:

*   For searches and help try:

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