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   Yogesh Uppal <[email protected]>
To   [email protected]
Subject   Re: st: appending several files with different variable names
Date   Fri, 2 Mar 2012 16:15:40 -0500

Hi Nick,

Thanks for your helpful suggestions and, through your experience,
warning about the extent of the problem. However, let me pose a
simpler problem with little bit more structure (which I hope is
clear). I will ignore the variables that are named in totally
arbitrary fashion for now.

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) and
run the following code (or some better version of it):

clear
set more off
cd "K:\my_folder"

local files: dir "`c(pwd)'" files "file_*.dta", respectcase
foreach file in `files'{
use `file'
if variable num_sch* exists {
rename num_sch* school
save `file'.dta, replace
clear
}
else if variable schools_num exists {
rename schools_num school
save `file'.dta, replace
clear
}
else if variable no_schools exists {
rename no_schools school
save `file'.dta, replace
clear
}
append using  `file'.dta
save `file'_country.dta, replace
}


I hope this makes sense. In any case, I am hoping I could learn
something I do not know.

Thanks,

Yogesh Uppal


On Thu, Mar 1, 2012 at 10:01 PM, Yogesh Uppal
<[email protected]> wrote:
> Dear Statalisters,
>
> I am appending multiple files each having over 100 variables, with
> same variables having different names in some files. Some of these
> variables have common strings in some files, but some others do not.
> Since the number of files and variables is large, I do not want to
> manually identify the variables that are same and give them some
> common name. I was wondering if there is a code that already exists to
> take care of issues like this.
>
> What I was leaning towards is creating a local macro for each file and
> use the command findname to look for some variable like var* and
> create a r(varlist) of such variables. And then run another loop over
> each file to rename each variable in r(varlist) to a specified name.
>
> I was hoping this method would rename variables that have common
> string var* (The problem I am facing here is that if some file does
> not have the variable name identified by var*, my loop gives me an
> error message).
>
> Is this a right way to go about the problem? If not, could you please
> suggest something better?
>
> Thanks in advance!!!
>
> Yogesh
> *
> *   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/


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