st: RE: AW: renaming using the file name

I would think about doing it a different way.
At the top of the program, initialise a file number
local f = 1
Make the -rename- command
rename price_ price_`f'
Put the _full_ file information in a variable label
label var price_`f' `"`file'"'
Bump up the file number after the -save-:
local ++f
I think this makes just one assumption that needs thought, that the file information will fit in a variable label. If that's a problem, think of using a characteristic.
Nick
n.j.cox@durham.ac.uk
Martin Weiss
You can get the filename via
*************
_getfilename "`c(filename)'"
di substr(r(filename),1,length(r(filename))-4)
*************
if you have a three letter suffix. I think there are better ways to do this,
but this is a first shot at the problem...
Linn Renée Naper
I am running the commands below, where I use a loop to insheet and change a
lot of files. The files are defined in `c(pwd)'.
Everything works well, it is just one problem.
Each file provides me with a new price variable price_ for the same set of
dates. Before I am
going to merge all the different price files I therefore need to give each
price an unique name. Obviously
I prefer to do this within the loop.
In the command rename price_ ..... is there any way I can give the variable
a name referring to the filename in use.
F.ex if the filename is priceA20071108.txt, can I tell stata to rename the
variable price_ price A, or simply A (thus to pick certain parts of the
filename in use)?
Thanks for any advice on this matter.
cd "C:\...\myfiles"
local files : dir "`c(pwd)'" files "*.txt"
foreach file in `files' {
clear
insheet using `file'
drop in 1/6
drop v26 v27 v28 v29 v30 v31 v32 v33 v34
renvars /.....
replace str_date= subinstr(str_date,".","-",.)
g double date = date(str_date, "DMY")
format date %td
g year=year(date)
keep if year==2007
destring price_*, replace
reshape long price_, i(date) j(hour)
collapse (mean)price_, by(date)
g year=year(date)
g month=month(date)
g day=day(date)
g week=week(date)
sort date
rename price_ "THIS IS WHERE I NEED HELP"
cd "C:\ \new_files"
save "`file'.dta", replace
cd "C:\...\myfiles "
}
*
* 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/