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: How to loop a <clean> script over multiple files?


From   "Dimitriy V. Masterov" <[email protected]>
To   [email protected]
Subject   Re: st: How to loop a <clean> script over multiple files?
Date   Fri, 20 Jan 2012 21:21:15 -0500

Here's some toy code that seems to work as long as the files are all
in the same directory. You should be able to modify it pretty easily.

I chose a slightly different strategy than you because I find it
easier to append multiple files than to merge them (though there are
ssc commands like nmerge and mergeall that you may want to look into).
You probably want the data in long format anyway.

Let me and the list know if you have any problems. I think regular
expression/subinstr combo could be improved upon, but I am am too
lazy.

DVM

*********************************************************
#delimit;
clear all;

cd "$lapdesktop"; // Change this!

/* Create Fake Data */
forvalues v=1/24 {;
	sysuse auto, clear;
 	outsheet using "data_file_`v'.txt", replace;
};


/* Import the txt data and do some renaming */
ssc install fs; // don't need to do it each time
fs data_file*.txt;

foreach txtfile in `r(files)' {;
	insheet using "`txtfile'", clear;
	local date "`=regexr("`=subinstr("`txtfile'",".txt","",1)'","[^0-9]+","")'";
// keep only the numberic part of filename
	rename make id;
	gen date=`date';
	save sd_`date'.dta, replace;
};


/* Combine the Stata Files Into One And Reshape */
fs sd*.dta;

clear;

append using `r(files)';

sort id date;

reshape wide price-foreign, i(id) j(date);


/* Erase both the txt and individual Stata file */
!rm data_file*.txt;
!rm sd_*.dta;
*********************************************************
*
*   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