Statalist


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

Re: st: Passing a local variable into a subsequent do-file


From   Sergiy Radyakin <serjradyakin@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Passing a local variable into a subsequent do-file
Date   Wed, 12 Aug 2009 20:38:56 -0400

Dear Bryce,

what you want in fact is globals. Locals are isolated to the
subroutine where they are created (and hence the name), so they are
not accessible unless passed as parameter, or via globals, or
matrices, or files, or otherwise.

Here is what you can do

// ---------- begin of file task.do
display "Relationship of mpg to weight, length and car origin in price
segment $PR_Q"
summarize mpg  if pq==$PR_Q
regress mpg weight length foreign if pq==$PR_Q
// ------------ end of file task.do


// ----------- begin of file main.do
sysuse auto
xtile pq=price, nq(5)
forvalues pq=1/5 {
  global PR_Q=`pq'
  do task.do
}
// ------------ end of file main.do


Also see -args-, locals `1', `2', etc, -syntax-.

Best regards, Sergiy Radyakin



On Wed, Aug 12, 2009 at 7:22 PM, Bryce Mason<bmason5@lmu.edu> wrote:
> Dear STATA Gods,
>
> Let me predicate this on the fact that I am by no means a great stata
> programmer. I'm constantly learning new things, so forgive my ignorance. I
> doubt I am going about things in the best way.
>
> I would like to know whether it is possible to pass a local variable to a
> subsequent do file called from my main do file. That is, I often call other
> do-files from a main file and use them like subroutines. It keeps my code
> shorter.
>
> Recently, however, I ran into a situation where the do file I am calling
> doesn't remember the local macro names I assigned in the main program. The
> local macro name in question stems from a foreach loop, so I can't use
> global assignment.
>
> Thank you for any help.
>
> Yours Sincerely,
>
> Bryce
>
>
> Bryce Mason, Ph.D.
> Director of Institutional Research
> Loyola Marymount University
> 1 LMU Drive | Los Angeles, CA 90045
>
> b:      4823 University Hall
> w:      (310) 258-8838
> f:      (310) 338-1841
> e:      bmason5@lmu.edu
>
>
>
>
> *
> *   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/
>

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index