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]

Re: st: new Stata package -lstrfun- lets you modify long local macros that contain strings using Mata string functions

From   Dan Blanchette <>
To   "" <>
Subject   Re: st: new Stata package -lstrfun- lets you modify long local macros that contain strings using Mata string functions
Date   Mon, 16 Aug 2010 15:06:46 -0400 (EDT)

Hello Eric,

Thanks for pointing that out.  Unfortunately, there's some
magic about publishing a program as I discovered that mistake
with my documentation of subinstr() and subinword() right as I was making my post to Statalist. I wish I had had you
as one of my beta-testers!  Since Kit Baum is away
until Aug. 26th, it's going to be a few weeks before I can
fix this.  Thankfully, it is just a documentation mistake as
-lstrfun- does run subinstr() and subinword() normally:

 . lstrfun m_test  subinstr(`"`m_test'"',"from", "to", n)

 . lstrfun m_test  subinword(`"`m_test'"',"from", "to", n)

Since Mata string functions do not have the ability to do
regex pattern matching, I fear trying to create that since
pattern matching can get extremely complex.  I think Stata
should add regex functions to Mata.  SAS has the ability
to run regex pattern matching on really long strings and do
multiple replacements.


On Mon, 16 Aug 2010, Eric Booth wrote:


Hey Dan:
Very cool new package.  One quick suggestion:

I think that these option descriptions in the help file:

subinstr(macname, "to", "from", n)
subinword(macname, "to", "from", n)

should be reversed to say:

subinstr(macname, "from", "to", n)
subinword(macname, "from", "to", n)

This seems to be the convention that is used elsewhere in Stata and this looks to be what -lstrfun- is already doing, ex:
local a  `c(pi)'
lstrfun new_mvar, subinstr(`"`a'"', "3", "[]", .)
di `"`new_mvar'"'
Also, I don't know how feasible it would be to add it, but it would be great to see some regex-type pattern matching allowed in some future version.
((I was able to get the options " strmatch(macname, pattern) " and " strpos(macname, "needle") "  to work together to let help substitute a string based on a pattern, but in order to get it to match/substitute every instance of a pattern, I used looping and this doesn't allow string matching the pattern at the beginning or end of the macro to be treated differently as in regex))

~ Eric
Eric A. Booth
Public Policy Research Institute
Texas A&M University
Office: +979.845.6754

On Aug 16, 2010, at 9:54 AM, Dan Blanchette wrote:

Dear Statalist Members,

Thanks to Kit Baum, a new Stata package -lstrfun- is now available for download from the SSC:

 . ssc install lstrfun


 -lstrfun- allows you to modify local macros that contain strings using Mata's
 string functions which are not restricted to the maximum length of strings
 in Stata like the normal string functions in Stata are limited.  For example:

 If the length of the local macro m_test is 1,234 and you want to make it all
 uppercase and you use the Stata string function upper():

   . local m_test= upper(m_test)

 then Stata will truncate m_test to the first 244 characters.  -lstrfun- will
 not truncate m_test:

   . lstrfun m_test, upper(`"`m_test'"')

 If you are using Stata SE or Stata MP, then your setting of maxvar affects the
 maximum number of characters in a macro.  The maximum number of characters in
 a macro is only 1,081,511 when maxvar is set to 32767.  Stata's c-class values
 will show you what your Stata session's limits are set to:

   . display c(maxvar)

   . display  %11.0gc c(macrolen)

   . set maxvar 27000

   . display  %11.0gc c(macrolen)

 -lstrfun- can be very helpful when trying to modify value labels and notes in
 Stata since values labels and notes are allowed to have more characters than
 normal string functions are allowed to handle.

Here is the help page for -lstrfun- in html format that shows all the string functions -lstrfun- can use to modify a local macro or create a new local macro:

Let me know if you have any questions,

Dan Blanchette
Research Associate
Center for Entrepreneurship and Innovation
Duke University's Fuqua School of Business

*   For searches and help try:

*   For searches and help try:

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