Bookmark and Share

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


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

st: Re: loop in variables labels


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
Subject   st: Re: loop in variables labels
Date   Mon, 15 Jul 2013 21:19:26 +0900

Haluk Vahaboglu wrote:

I have some a *.dta file that is imported from an *.xls with some 30 variables
named as _3_DAYS_X_X; _6_DAYS_X_X; _9_DAYS_X_X;_3_WEEKS_X_X; _6_WEEKS_X_X;
_9_WEEKS_X_X and so on.
I want to rename all these var names using a foreach loop to DAYS_3;
DAYS_6...;WEEKS_3... and so on.
I could not find a help in FAQ or internet search.
I would appreciate any help 

--------------------------------------------------------------------------------

If you're using Stata Release 12 or 13, then you can accomplish what you want in
a single line of code.  

Type

    help rename group

at the Stata command line for more information.

Joseph Coveney

. set obs 1
obs was 0, now 1

. 
. foreach var of newlist _3_DAYS_X_X _6_DAYS_X_X _9_DAYS_X_X ///
>         _3_WEEKS_X_X _6_WEEKS_X_X _9_WEEKS_X_X {
  2.         generate float `var' = 0
  3. }

. list, noobs abbreviate(20)

 
+-------------------------------------------------------------------------------
-------+
  | _3_DAYS_X_X   _6_DAYS_X_X   _9_DAYS_X_X   _3_WEEKS_X_X   _6_WEEKS_X_X
_9_WEEKS_X_X |
 
|-------------------------------------------------------------------------------
-------|
  |           0             0             0              0              0
0 |
 
+-------------------------------------------------------------------------------
-------+

. 
. 
. *
. * Begin here
. *
. rename (_*_DAYS* _*_WEEKS*) (DAYS_* WEEKS_*)

. 
. list, noobs abbreviate(20)

  +--------------------------------------------------------+
  | DAYS_3   DAYS_6   DAYS_9   WEEKS_3   WEEKS_6   WEEKS_9 |
  |--------------------------------------------------------|
  |      0        0        0         0         0         0 |
  +--------------------------------------------------------+

. 
. exit

end of do-file

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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