Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Sergiy Radyakin <serjradyakin@gmail.com> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: RE: loop in variables labels |
Date | Mon, 15 Jul 2013 09:38:38 -0400 |
1) anynumber is assumed to be integer 2) you have to make sure that the target variables do not exist yet. Best, Sergiy sysuse auto, clear generate _31anyword_anyword_anyword=1 describe mata vc=0 for(i=1;i<=st_nvar();i++) { v=st_varname(i) t=tokens(v,"_") if (cols(t)==6 & t[1,1]=="_") { c=1 while(min(ascii(substr(t[1,2],1,c)))>=48 & max(ascii(substr(t[1,2],1,c)))<=57) c++ vn=substr(t[1,2],c,strlen(t[1,2])-c+1) c=c-1 vn=vn+"_"+substr(t[1,2],1,c) stata("rename "+v+" "+vn) vc++ } } printf("{text}Renamed {result:%g} variables",vc) end describe //end of file On Mon, Jul 15, 2013 at 8:19 AM, Haluk Vahaboglu <vahabo@hotmail.com> wrote: > Thank you Matt, > Sure it works but unfortunately my variable numbers are not in a discipline as given in the example. > In my variable names there are leading "_" s followed by a "number" and "word" "_" "word".... > Here I want to take the first "number" and put it after the first "word" to get a sequence of "word""_""number" and do this by means of a loop. Do whatever my number or word is. > Thanks again. Have to wait some more help.. > > > Prof. Dr. Haluk Vahaboğlu > Istanbul Medeniyet > Üniversitesi, > Göztepe Eğitim ve Araştırma Hastanesi > Enfeksiyon Hastalıkları > ve Klinik Mikrobiyoloji ABD > Dr. Erkin Caddesi 34730 > Kadıköy / Istanbul TURKIYE > > > >> From: matthewbarclay@nhs.net >> To: statalist@hsphsun2.harvard.edu >> Date: Mon, 15 Jul 2013 13:01:43 +0100 >> Subject: st: RE: loop in variables labels >> >> Hello Haluk, >> >> I would try: >> >> foreach number in 3 6 9 { >> rename _`number'_DAYS_X_X DAYS_`number' >> } >> foreach number in 3 6 9 and so on { >> rename _`number'_WEEKS_X_X WEEKS_`number' >> } >> >> You could do this in one nested loop, but that will become complicated if you have more WEEKS variables than DAYS variables. There might be a way to do this in one line using -renvars- (SSC), but even though I do this kind of rename frequently I think it's easier to write a quick loop than figure out the correct syntax. >> >> Hope that helps, >> Matt >> >> >> -----Original Message----- >> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Haluk Vahaboglu >> Sent: 15 July 2013 12:35 >> To: statalist@hsphsun2.harvard.edu >> Subject: st: loop in variables labels >> >> Dear listers, >> >> 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 >> >> Regards >> >> Prof. Dr. Haluk Vahaboğlu >> Istanbul Medeniyet >> Üniversitesi, >> Göztepe Eğitim ve Araştırma Hastanesi >> Enfeksiyon Hastalıkları >> ve Klinik Mikrobiyoloji ABD >> Dr. Erkin Caddesi 34730 >> Kadıköy / Istanbul TURKIYE >> >> * >> * 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/ >> >> ******************************************************************************************************************** >> >> This message may contain confidential information. If you are not the intended recipient please inform the >> sender that you have received the message in error before deleting it. >> Please do not disclose, copy or distribute information in this e-mail or take any action in reliance on its contents: >> to do so is strictly prohibited and may be unlawful. >> >> Thank you for your co-operation. >> >> NHSmail is the secure email and directory service available for all NHS staff in England and Scotland >> NHSmail is approved for exchanging patient data and other sensitive information with NHSmail and GSi recipients >> NHSmail provides an email address for your career in the NHS and can be accessed anywhere >> >> ******************************************************************************************************************** >> >> * >> * 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/ > > * > * 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/ * * 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/