Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Passing value of an observation to a user-written function fails


From   daniel klein <[email protected]>
To   [email protected]
Subject   Re: st: Passing value of an observation to a user-written function fails
Date   Sun, 27 May 2012 18:48:56 +0200

Phil,

note that what you have is not a function, but a (r-class) program.
You may not write your own functions in Stata, for that use Mata.

I also do not see the need to loop over observations here, since Stata
oparates row-wise per default.

forv j = 1/`= _N' {
    replace <oldvar> = <somevar[`i']> <operator> <anothervar[`i']> in `i'
}

is exactly the same as

replace <oldvar> = <somevar> <operator> <anothervar>

The only difference is, that the second line of code will execute much faster.

If you decide you really need the (very slow) loop, evaluating the
arguments before passing them to your program might solve your
problem.

Instead of

atc start_time[`i'] end_time[`i']

code

atc `= start_time[`i']' `= end_time[`i']'

Best
Daniel

-- 
I have a user-written function "atc.ado" which accepts two integers as
arguments [...] The return value of my function is in r(units).

[...]The code
needs to execute independently for each observation, as the times are
different for each observation. [...]

Can anyone help me? I'm sure there is something simple I can do to
solve this problem, but I'm at a loss.

Many thanks!

Phil
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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