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   "Andrew Felton" <[email protected]>
To   [email protected]
Subject   Re: st: Extracting a variable of a varliable list
Date   Mon, 25 Jul 2005 20:48:24 -0000

You shouldn't put quotes around the macro in the extended function
-word of-.  e.g.,

. local varlist "var1 var2 var3 var4"

. local depvar : word 1 of "`varlist'"

. di "`depvar'"
var1 var2 var3 var4

. local depvar : word 1 of `varlist'

. di "`depvar'"

--- In [email protected], David Kantor <dkantor@j...> wrote:
> 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
>>the independent variables. I need to access them seperately in my
>>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
>>local depvar=word("`varlist'",1)
>>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-
>>can only be applied to strings up to 81 characters.
>>I tried also to precede 'varlist' in the -syntax- command by
>>'name' or 'anything' to capture the independent variable, but that
>>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
> 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
> extended functions.  See -help local-, and loot at the "extended_fcn"
> section.  Often, a function also exists as an extended function for
> and you can just switch the = to a : (colon) -- though sometimes the
> 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
> 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.
> */
> HTH,
> -- David
> David Kantor
> Institute for Policy Studies
> Johns Hopkins University
> dkantor@j...
> 410-516-5404
> *
> *   For searches and help try:
> *
> *
> *

*   For searches and help try:

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