Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

st: RE: RE: How to loop a <clean> script over multiple files?

From   Nick Cox <>
To   "''" <>
Subject   st: RE: RE: How to loop a <clean> script over multiple files?
Date   Mon, 23 Jan 2012 18:55:12 +0000

As already pointed out, -fs- (SSC) does all this for you. 


Impavido, Gregorio

If the files can be differentiated by a specific element try this for point 1 of your question:

local wkdir "/Users/gregorio/Documents/Stata" // or whatever is your working directory
local filelist : dir . file "`wkdir'/eu??.txt"  // builds macro with all relevant files of type "eu01.txt"

you can then run something like 
foreach file of local filelist {
	insheet using etc.....

-----Original Message-----
From: [] On Behalf Of Brandon Olszewski
Sent: Friday, January 20, 2012 7:38 PM
Subject: st: How to loop a <clean> script over multiple files?

Hi Statalist:
I want to clean about 24 data files (all with the same variables)
elegantly, without pasting the clean code for each one separately. I'm
starting with .txt files organized by month, will clean them, save
them as .dta files in a temp directory (mkdir  ${datpath}temp), merge
them into a wide file, then reshape them into a long file. To
simplify, let's say the cleaning code is this:
insheet using ${datpath}data_file_1.txt
rename var1 id
format id %9.0g
renvars _all, postfix(1)
rename id id
save ${datpath}temp/data_file_1.dta

I want to run that code on 24 files (representing 2 years' worth of
monthly data), creating files that have a constant id at each time,
but variables that are marked by time (hence, the renvars/postfix).
Obviously, I know how to set up a global macro for my data path, but I
don't know enough to apply that to my problem here. It seems like I
need to:
1. Write a macro that identifies the 24 files (maybe with
"${datpath}*.txt" ?) so that the .do file knows what files to insheet
and clean;
2. Write a macro that tells Stata to add the correct <postfix> to each
file - I imagine that's done with something like:
local i = 1
renvars _all, postfix(i+1)
...but I know that won't work, because it will tag all the variables
with a "2" at the end or something similar.

So, I'm stumped. I've looked on the list, for instance at and at
the <Programming Stata> FAQ page at, but I don't even know where
to begin. I write <foreach> loops all the time, but I don't understand
the mechanics well enough to apply those ideas to this problem. Thanks
for any help, in advance.

*   For searches and help try:

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