Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <n.j.cox@durham.ac.uk> |
To | "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> |
Subject | st: RE: Passing value of an observation to a user-written function fails |
Date | Mon, 28 May 2012 12:03:05 +0100 |
This has been solved to Philip's satisfaction, but if the command -atc- (which is otherwise unidentified) is used at all frequently it could be worthwhile re-writing it to produce one variable from two. Being dependent on using it in this way will remain awkward. Nick n.j.cox@durham.ac.uk Philip Jones 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. * * 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/