Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Testing/comparing macro variables containing long string data


From   Dan Blanchette <[email protected]>
To   [email protected]
Subject   st: Testing/comparing macro variables containing long string data
Date   Tue, 8 Jan 2008 14:52:31 -0500 (EST)

I am making this post in order to increase the number of Stata
programmers who know about Stata's extended macro function called "list". This macro function lets you do a fair number of interesting things with macros in Stata. If you write ado-files that manipulate macros with string information it's well worth checking out. In fact, it is a good if not _necessary_ habit to develop if you want to compare local/global macros that contain string information since Stata will only compare the first 80/244 characters (maximum string length) if your macro
variable contains more characters and you use the normal coding
style using two equal signs to compare the two macro variables:

if "`mvar1'" == "`mvar2'" {
display "mvar1 is the same as mvar2!"
}

You really need to use the local macro extended function "list":

// test1 will equal either 0 or 1
// 0 = false 1 = true
local test1 : list mvar1 == mvar2

if `test1' == 1 {
display "mvar1 is the same as mvar2!"
}

But, since the "list" extended macro function assumes that your macro names are local you should specify it:

if `: list local(mvar1) == local(mvar2) ' {
display "mvar1 is the same as mvar2!"
}

as this is how you do global macros:
if `: list global(mvar1) == global(mvar2) ' {
display "mvar1 is the same as mvar2!"
}


If you create a more complex if condition that contains more than two macro variables and do not use the "list" extended macro function and you have strings longer than 160 and less than 244 characters (which should be fine) like:

if "`mvar1'" != "`mvar2'" & ///
"`mvar1'" != "`mvar3'" {
display "mvar1, mvar2, and mvar3 are all different!"
}

and at least two of the macro variables are not the same. You likely get an error message of :

too many literals
r(130);

If two of the macro variables are the same like:

local mvar2 "`mvar1'"
if "`mvar1'" == "`mvar2'" & ///
"`mvar1'" != "`mvar3'" {
display "It's true."
}

Stata is fine.

So, oddness happens even when you compare macro variables that are inside the limits of the maximum number of characters of strings for your version of Stata. If you
make using the "list" extended macro function a habit, you will avoid accidentally creating code that does not give an
error message and also does not do what you think it's doing.

I hope this macro tip solves some problems.

Dan Blanchette

Research Computing
University of North Carolina Chapel Hill
[email protected]

*
* 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/




© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index