Statalist The Stata Listserver


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

Re: st: Using scalars when defining matrices


From   Rachel <[email protected]>
To   [email protected]
Subject   Re: st: Using scalars when defining matrices
Date   Fri, 8 Dec 2006 10:17:58 -0500

Thanks for all your replies.  I have tried several solutions,
including using local variables, but nothing has worked.  The relevant
lines of my ado file are as follows:

1:arg {other vars} abc
2: tempname beta BigMat
3 scalar `beta'=exp(`abc')
4 matrix input `BigMat'=(1,`scalar(beta)'\`scalar(beta)',`scalar(beta)')

When I trace the program, stata is reading line 4 as

"= matrix __00001A=(1,,,\ ,1,0,0"  and gives me the error message "1, not found"

The problem, I suppose, is that I'm not clear on how to use scalars in
conjunction with tempnames.  I have read the [P] manual on scalars,
but they are not specific on this point.

(I have tried various other ways to write line 4 (i.e. scalar(`beta')
instead of `scalar(beta)').  I"ve also tried to define `beta' as a
local

scalar `beta'=exp(`abc')

but this is obviously problematic because it evaluates `beta' as the
string "exp(`abc').)

Thanks again for all your help.




On 12/7/06, Michael S. Hanson <[email protected]> wrote:
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/

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