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

st: AW: renaming using the file name

From   "Martin Weiss" <>
To   <>
Subject   st: AW: renaming using the file name
Date   Tue, 21 Apr 2009 17:17:13 +0200


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...


-----Ursprüngliche Nachricht-----
[] Im Auftrag von Linn Renée
Gesendet: Dienstag, 21. April 2009 17:07
Betreff: st: renaming using the file name

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' {

	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:

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index