Bookmark and Share

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

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

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

From   Philip Jones <>
To   Statalist <>
Subject   st: Passing value of an observation to a user-written function fails
Date   Sun, 27 May 2012 11:39:40 -0400

Hi all,

I have a user-written function "atc.ado" which accepts two integers as
arguments (which are actually times, like "1135" or "1232"), and
outputs a number (time units according to a formula, depending on the
duration). The return value of my function is in r(units).

For each observation in my dataset, I want to be able to run my
user-written function and create a variable called "units". The code
needs to execute independently for each observation, as the times are
different for each observation. I have not found a way to set a
variable's value to the return value of a user-written function
automatically, so I was trying the following code to loop through each
observation in the dataset:

generate units = .
local N = _N
forvalues i = 1/`N' {
	quietly atc start_time[`i'] end_time[`i']
	replace units = r(units) in `i'

However, this fails due to "invalid syntax". I have narrowed the
problem down to the fact that passing "start_time[`i']" to my
user-written function causes this error, even though "display
start_time[`i']" shows the correct number. The format of whatever is
getting passed to my function is not recognized properly, and my
function fails its "args m1 m2" line (which defines what the function
is expecting as arguments).

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!

*   For searches and help try:

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