Statalist The Stata Listserver


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

Re: st: Using scalars when defining matrices


From   "Michael S. Hanson" <[email protected]>
To   [email protected]
Subject   Re: st: Using scalars when defining matrices
Date   Thu, 7 Dec 2006 23:07:44 -0500

On Dec 7, 2006, at 7:48 PM, Rachel wrote:

I'm having trouble with the syntax of matrix define (or input) when
using scalars. When I try the following:

scalar b3=2
matrix input Matrixnew=(1,`b3',`b3',`b3'\ `b3',1,0,0\ `b3',0,1,0\ `b3',0,0,1)
	[snip]

What am I doing wrong?
You are confusing scalars with local macros. Consider the following:

scalar b3 = 2
local b3 = 4
matrix Ms = (1,b3,b3,b3 \ b3,1,0,0 \ b3,0,1,0 \ b3,0,0,1)
matrix Ml = (1,`b3',`b3',`b3' \ `b3',1,0,0 \ `b3',0,1,0 \ `b3',0,0,1)
mat list Ms
mat list Ml

Notice that b3 -- the scalar -- and b3 -- the local macro -- are two *different* objects with two *different* values. That may be confusing to you, but Stata understands the difference. And it's important it does, otherwise programs (do and ado files) could accidentally clobber a variable, scalar, matrix, etc. that might exist in the user's active memory prior to a program being executed if it just so happened that they had the same name.

As long as you are consistent -- that is, use a scalar and do not quote it, or use a local macro and quote it appropriately -- you likely will not get confused either. I find that using scalars is more convenient when working interactively in Stata, but for any kind of programming (i.e. do or ado files), local macros are well worth learning and using. The manuals are the best place to learn this topic -- far more complete than the on-line help.

-- Mike

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