Statalist


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

Re: st: Re: program to generate variable list as input for other program


From   Bob <[email protected]>
To   [email protected]
Subject   Re: st: Re: program to generate variable list as input for other program
Date   Mon, 11 Jan 2010 01:55:07 -0800 (PST)

Thanks a lot for the solution, Joseph and Maarten!

In principle, my script works now but it is very slow, I would say like factor 20 to the version of the program where all variable names are spelled out and no loops are used. The program is used in an evaluator function for a regression and thus called many times. I suppose it has to do with for loops and the frequent string replacements coming with the macro replacements of the `var's. I am using a lot of macros to do a number of calculations. 

Is there a better way of getting something equivalent to "variables" in other programming languages so that I can reduce the string replacements? 
Is there a guide on how to do efficient programming and flow control in STATA?

Currently, I have only the idea to generate the completely spelled out program code, without loops and additional macro replacements, with another programming language and then run the script in STATA. But this shouldn't be the preferred way.

I am very grateful for any help in this direction.

Many thanks,
  Bob



--- On Sat, 1/9/10, Joseph Coveney <[email protected]> wrote:

> From: Joseph Coveney <[email protected]>
> Subject: st: Re: program to generate variable list as input for other program
> To: [email protected]
> Date: Saturday, January 9, 2010, 11:50 PM
> Bob wrote:
> 
> I am new to Stata and I want to programmatically generate a
> list of variable
> names in a program which I want to hand over to another
> program as parameters.
> Unfortunately, if I hand the returned macro over as a
> string, Stata says "
> invalid name" and if I don't do that, then the list of
> variables is converted to
> values which is also unwanted.
> 
> --------------------------------------------------------------------------------
> 
> Try an approach like that illustrated below.
> 
> Joseph Coveney
> 
> ============begin do-file==============
> 
> version 11.0
> 
> clear *
> set more off
> 
> program define sender, rclass
>     version 11.0
>     syntax
> 
>     local variable_list
>     foreach var of varlist _all {
>         capture confirm
> numeric variable `var'
>         if (!_rc) {
>            
> local variable_list `variable_list' `var'
>         }
>         else {
>            
> continue
>         }
>     }
> 
>     return local varlist `variable_list'
> end
> 
> program define receiver
>     version 11.0
>     syntax varlist
> 
>     summarize `varlist'
> 
>     display in smcl as text _newline(2)
> "`varlist'"
> end
> 
> sysuse auto
> 
> sender
> receiver `r(varlist)'
> 
> exit
> 
> ============end do-file==============
> 
> 
> *
> *   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index