Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Yogesh Uppal <stata.list.queries@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: appending several files with different variable names |
Date | Tue, 6 Mar 2012 11:15:41 -0500 |
Dear Daniel, Thanks for your response and the code. I tried what Nick suggested for the variables that did not have any common strings. It will be nice to automate the process for other variables using your code. I will let you how it works. Many thanks, Yogesh On Sat, Mar 3, 2012 at 2:58 AM, daniel klein <klein.daniel.81@googlemail.com> wrote: > Yogesh, > > aside from the fact that several lines in your pseudo code will > probably not do what you might expect, I understand that you want to > create _one_ file. I also understand you want to -rename- _one_ > variable that has *school* in its name to "school" in each file. > > Here is something you migth want to try. I am not sure about the > double qoutes used. If your file names have embeded spaces you need to > take this into account. Staring from you loop, when you already have > the file names in a local, try > > foreach file in files { > qui u `file' ,clear > cap noi unab school : *school* > if _rc { > di as err "error occurred in `file'" > continue > } > if (`: word count `school'' > 1) { > di as txt "(note: more than one " /* > */ "variable *school* found in `file'" > [do whatever you want to do in this case] > } > cap noi ren `school' school > loc `"foundin `foundin' "new_`file'""' > sa new_`file' > } > gettoken firstfile foundin : foundin > u `firstfile' ,clear > ap using `foundin' > sa file_country > > Best > Daniel > > -- > Hi Nick, > > [...] > Suppose I have a bunch of files to append. Each of these files is for > a state and named file_* (e.g., file_AK.dta, file_AL.dta and so on). > There is a variable that measures number of schools in each state. It > is named num_schools in some files, schools_num or no_schools in > others. Also, to make the matter worse, some files do not have this > variable. What I was hoping is a way to collect all such variables > that can be identified by a common string (school in this case) > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/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/statalist/faq * http://www.ats.ucla.edu/stat/stata/