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

st: RE: Extracting variable names for use as string lists


From   "Jann, Ben" <[email protected]>
To   <[email protected]>
Subject   st: RE: Extracting variable names for use as string lists
Date   Thu, 14 Jul 2005 16:12:11 +0200

David asked:
> Is there any way to create a list of variable names, either 
> in string form 
> or some other way, that could then be manipulated using 
> "foreach" and the 
> like?

See -help unab-. However, -foreach- can also do this directly,
e.g.

 foreach x of varlist * {
 ...
 }

will loop over all variables in the dataset.

> I would like to rename multiple variables in a consistent way 
> -- that is, to 
> abbreviate their names to 8 characters if they are any longer 
> than this 
> (using 'abbrev').  I would like to use a structure something like:
> 
> foreach x in [list of variable names] {
> 	if length(`x')>8 {
> 		rename `x' abbrev(`x',8)
> 		}
> 	}

How about this:

 foreach x of var * {
  if length("`x'")>8 {
   local newname: permname `x', length(8)
   rename `x' `newname'
  }
 }
 
Cheers,
ben

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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