Statalist


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

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


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
Subject   st: Re: program to generate variable list as input for other program
Date   Sun, 10 Jan 2010 13:50:41 +0900

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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index