Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Extracting a variable of a varliable list

From   David Kantor <[email protected]>
To   [email protected]
Subject   Re: st: Extracting a variable of a varliable list
Date   Mon, 25 Jul 2005 12:36:27 -0400

At 05:43 PM 7/25/2005 +0200, Thomas Cornelissen wrote:
Dear Stata Users,
I want to write an ado file using the -syntax- command as follows:

syntax varlist(min=2) [if] [in]

The first variable in varlist is the dependent variable, the others are the independent variables. I need to access them seperately in my ado-file.
Therefore I create a local that contains the name of the dependent variable, i.e. the first word of the varlist-string, and a local that contains all the rest and can be used as a varlist of only the independent variables.

local depvar=word("`varlist'",1)

local indepvar=substr("`varlist'",length("`depvar'")+1,length("`varlist'"));

My problem is that if the varlist is very long, it gets cut off at the 81st character. It seems that both the -length- and the -substr- function can only be applied to strings up to 81 characters.

I tried also to precede 'varlist' in the -syntax- command by 'varname', 'name' or 'anything' to capture the independent variable, but that returns error messages.

Can anybody suggest a better way to do what I want to do? Is there a good way of splitting one varlist into two varlists?
Thanks a lot for any suggestions!

Thomas Cornelissen
Empirical Economic Research
University of Hannover, Germany
The problem is that you are setting the macros to expressions -- using an equal sign. String expressions are limited to 80 characters (or something longer in Stata SE).

If instead, you use extended functions, this truncation will not occur (at least, not for a few thousand characters). You should look into the macro extended functions. See -help local-, and loot at the "extended_fcn" section. Often, a function also exists as an extended function for macros, and you can just switch the = to a : (colon) -- though sometimes the syntax is a bit different (as shown below).

But in your case, there are possibly better options.

local depvar : word 1 of "`varlist'"
/* Note the colon and the somewhat different syntax. You had local depvar=word("`varlist'",1), which is probably okay, because of the length of a variable name, but this is, generally, more secure.

local indepvar : list varlist - depvar

/* -- gets you what you want directly. See -help macrolists-.
There are other solutions, but this is probably the simplest in terms of programming, and it is very comprehensible.

-- David

David Kantor
Institute for Policy Studies
Johns Hopkins University
[email protected]

* For searches and help try:

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