Statalist


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

RE: st: Re: read tempvars in user created programs


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Re: read tempvars in user created programs
Date   Wed, 19 Nov 2008 11:49:59 +0100

Ok. BTW, you seem to have set - #delimit ;- in your code (which might
confuse readers...) 

HTH
Martin


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of mario fiorini
Sent: Wednesday, November 19, 2008 11:43 AM
To: statalist@hsphsun2.harvard.edu
Subject: RE: st: Re: read tempvars in user created programs

Hi Martin,
no indeed the subroutine is not be able to match `TV`price'' to the tempvar.
You have to pass the tempvar (and not just the name of the handle) if you
want the routine to read the temporary variable. The code below works but I
am now passing the full tempvar as an argument to the program. I am not
fully sure why the handle's name did not work though. 

Mario

 // ===================

cap program drop progtempvar;
program define progtempvar;
 args progvars;
 di "`progvars'";
 su `progvars';
end;

sysuse auto;
tempvar TVprice; ge `TVprice'= price;
progtempvar `TVprice';

 // ======================

> From: martin.weiss1@gmx.de
> To: statalist@hsphsun2.harvard.edu
> Subject: RE: st: Re: read tempvars in user created programs
> Date: Wed, 19 Nov 2008 09:31:23 +0100
> 
> Line for the server...
> 
> Mario,
> 
> Does Sergiy`s code address your problem in your initial post that seemed
to
> consist in prepending "TV" automatically to the name of the -tempvar-? I
> played around a lot last night with this and could not tell Stata that it
> should add "TV" to "price" and then interpret the whole thing as a
> -tempvar-...
> 
> HTH
> Martin
> 
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of mario fiorini
> Sent: Wednesday, November 19, 2008 4:37 AM
> To: statalist@hsphsun2.harvard.edu
> Subject: RE: st: Re: read tempvars in user created programs
> 
> thanks Sergiy!
> I could not figure out that passing just the handle of the temporary
> variable would not work. 
> 
> Mario
> 
> 
> 
>> Date: Tue, 18 Nov 2008 22:12:23 -0500
>> From: serjradyakin@gmail.com
>> To: statalist@hsphsun2.harvard.edu
>> Subject: Re: st: Re: read tempvars in user created programs
>> 
>> Yes Mario,
>> 
>> the variables will be there. You can check this by calling -describe-
>> within your subroutine. However you must somehow tell the subroutine,
>> which variables you keep in mind (and for the subroutine they will not
>> be temporary, they will exist after the subroutine completes because
>> they were created before the subroutine started). If you pass the name
>> of the handle of the temporary variable the subroutine will not be
>> able to dereference it. To correct the problem, you must dereference
>> in the calling routine (the one that created the tempvar, or more
>> correctly that obtained a handle, potentially you can create the
>> variable itself in the subroutine), see example below. Using globals
>> is feasible but very highly unrecommended and should be avoid at all
>> means possible.
>> 
>> Hope this helps.
>> 
>> Best regards,
>>    Sergiy Radyakin
>> 
>> version 9.2
>> clear
>> program drop _all
>> 
>> program define summ_temp_var /* this is subroutine */
>>    args myvar
>>    describe
>>    sum `myvar'
>>    display "I have no idea what [`z'] means, because I have no access
>> to local z from here,"
>>    display "but I can see globals from anywhere, so I know that global
>> z is [$z]"
>>    sum $z
>> end
>> 
>> program define test_summ_temp_var /* this will create a tempvar */
>>   tempvar z  /* z is not accessible from the subroutine */
>>   global z `z'
>>   generate `z'=uniform()
>>   display "Tempvar z refers to `z'"
>>   summ_temp_var `z'  /* pass the value of z, not the name z itself */
>>   sum `z' /* variable still exists, it was not destroyed when the
>> summ_temp_var has finished */
>>   describe
>> end
>> 
>> sysuse auto
>> test_summ_temp_var
>> describe
>> 
>> 
>> 
>> 
>> On Tue, Nov 18, 2008 at 9:31 PM, mario fiorini  wrote:
>>> Hi Martin,
>>> yes I know that. I am mainly trying to understand whether there is a way
> to have a program that sees tempvars generated externally to the program
> itself. The summarize command is there just as an example.
>>>
>>> Mario
>>>
>>>> From: martin.weiss1@gmx.de
>>>> To: statalist@hsphsun2.harvard.edu
>>>> Subject: st: Re: read tempvars in user created programs
>>>> Date: Wed, 19 Nov 2008 00:59:54 +0100
>>>>
>>>> Well,
>>>>
>>>> -summarize- never sees the -tempvar- that you intend to use; instead it
> sees
>>>> nothing. If -su- sees nothing, it -summarize-s everything by default,
>>>> including your -tempvar-...
>>>>
>>>> HTH
>>>> Martin
>>>> _______________________
>>>> ----- Original Message -----
>>>> From: "mario fiorini"
>>>> To:
>>>> Sent: Wednesday, November 19, 2008 12:44 AM
>>>> Subject: st: read tempvars in user created programs
>>>>
>>>>
>>>>> Dear all,
>>>>> I have written a program to produce some test statistics. However, the
>>>>> program does not seem able to use the temporary variables
>>>>> that I've generated outside of it. For illustrative purposes consider
> the
>>>>> following lines, where I define a program to summarize a tempvar (my
> real
>>>>> problem is more complex and that's why I use a program).
>>>>>
>>>>> // ===================
>>>>>
>>>>> cap program drop progtempvar;
>>>>> program define progtempvar;
>>>>>    args progvars;
>>>>>    di "`progvars'";
>>>>>    su `TV`progvars'';
>>>>> end;
>>>>>
>>>>> sysuse auto;
>>>>> tempvar TVprice; ge `TVprice'= price;
>>>>> progtempvar price;
>>>>>
>>>>> // ======================
>>>>>
>>>>> The program would not recognize the temporary variable. Is there a way
> to
>>>>> get around this without creating a non-temporary variable?
>>>>> (that is why  " reg price `TVprice' " work instead?) Thanks in
advance,
>>>>>
>>>>> Mario
>> *
>> *   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/
> 
> _________________________________________________________________
> Time for change? Find your ideal job with SEEK.
>
http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Fninemsn%2Eseek%2Ecom%2Eau%2F
>
%3Ftracking%3Dsk%3Atl%3Ask%3Anine%3A0%3Ahottag%3Achange&_t=757263783&_r=SEEK
> _tagline&_m=EXT
> *
> *   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/

_________________________________________________________________
Take a summer road trip with Windows Live Hotmail. Multiple prizes and the
ultimate dream beach house!
http://www.ninemsn.com.au/hotmailroadtrip
*
*   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