Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

RE: st: RE: Defining input and output variables when calling a program

From   Nick Cox <>
To   "''" <>
Subject   RE: st: RE: Defining input and output variables when calling a program
Date   Wed, 16 Feb 2011 14:59:04 +0000

The syntax -, Generate(newvarlist)- is what you would tell the user. 

Internally it might look something like this 

syntax varlist , Generate(str)


capture Checknewvarlist `generate' 
if _rc { 
	di as err "generate() problem: " _c
	error _rc



program Checknewvarlist 
	syntax newvarlist 
	c_local generate `varlist' 

c_local is a non-documented command that pushes the newvarlist back into the main program inside the local macro -generate-, unabbreviated if necessary. So, when you -generate- you can loop over the words inside that macro. 

For another way to do it, look inside -rowsort- (SJ). But beware: -syntax- calls zap macros created by a previous -syntax call inside a program. That's why I delegate the task here to a subprogram. 

Another way to do it would be for the subprogram to be s-class. 


Svend Juul

This looks perfect, Nick. My problem is, I don't know how to get there. Could you give a hint?

Nick Cox

This isn't impossible, but first let's explore another possibility.
How do you feel about the syntax
svendsational varlist, Generate(newvarlist)
? -- because that's relatively easy.

Svend Juul

I want to develop a command (ado-file) that takes as the input the responses to a number of questions and as output generates new variables, much as the reponses to the 36 questions in the SF-36 questionnaire are used to generate 8 scales. I want the command to be flexible; the names of the input and output variables being defined when calling the program, not by modifying the program.
If the program name is xxx, the input variables are q1-q15 and the output variables s1-s5, I want to be able to call the program by something like:
  . xxx q1-q15 s1-s5
The following is not legal; apparently you cannot have both a -varlist- and a -newvarlist-:
  . syntax varlist(min=15 max=15) newvarlist(min=5 max=5)
If I combine the input and output variables in a single -varlist- I get an error message because all variables must exist in the dataset beforehand:
  . syntax varlist(min=20 max=20)
... - unless I generate the output variables (e.g., as missing) before calling the program, but that seems to me to be a clumsy solution.

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index