Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: appending several files with different variable names


From   daniel klein <klein.daniel.81@googlemail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: appending several files with different variable names
Date   Sat, 3 Mar 2012 08:58:27 +0100

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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index