Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Variable number of matrixes as arguments

From   Richard Williams <[email protected]>
To   [email protected]
Subject   Re: st: Variable number of matrixes as arguments
Date   Tue, 26 Jul 2005 23:35:32 -0500

At 06:01 PM 7/26/2005 -0400, Herb Smith wrote:

But what if I want the routine to return, on occasion, some values
calculated over these matrixes such as (for simplicity!) the number
of non-zero cells in the new matrix, B?  Assume that this number is
being calculated within the -gprvector- routine in the local `cells':

How do I return this result along with the new matrix, B?  I tried (by

        syntax, input(name) output(name) [cells(name)]

and then executed

        gprvector, input(A) output(B) cells(z)

but the follow-up command

        dis z

draws a blank.
This is hard to answer without seeing more code and testing it out, but following are my guesses on this.

First off, it is always possible z never got assigned anything, so you could try -set trace on- to make sure your program is doing what you want it to.

Second, within the program, what does the code for `cells' look like, i.e. how are you assigning it a value? I think it should be something like

scalar `cells' = some number or another

My guess (quite possibly wrong) is that you are using local commands instead, e.g. maybe you have

local `cells' = some number or another

which expands to

local z = some number or another

and `z' (which is not the same as z) disappears after the routine is run.

But if that isn't it -- It is a little lazy, but if I am having trouble getting my routines to communicate with each other I use global variables. So, within -gprvector-, have something like

global z = some number or another

After having executed -gprvector-, you can give the command

display $z

I suspect the most common way to get what you want is to set your program up as rclass, sclass or eclass. So the program might look something like

program define gprvector, rclass
... do bunch of stuff
local z = some number or another
... more stuff
return local cells `z'
... finish program

Back to main program or interactive run:

display r(cells)

Richard Williams, Notre Dame Dept of Sociology
OFFICE: (574)631-6668, (574)631-6463
FAX: (574)288-4373
HOME: (574)289-5227
EMAIL: [email protected]
WWW (personal):
WWW (department):

* For searches and help try:

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