Statalist


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

st: renaming using the file name


From   Linn Renée Naper <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: renaming using the file name
Date   Tue, 21 Apr 2009 17:07:29 +0200

Dear Statalist
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 "

}

Linn

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index