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]

Re: st: appending several files with different variable names


From   daniel klein <[email protected]>
To   [email protected]
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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index