Import multiple files automatically
Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt
Import multiple files automatically
Hi everyone,
I am trying to get EViews to import multiple files into the same workfile using a loop that accounts for changing filenames and dynamically names colums, if possible.
I have files with automatically created filenames according to the rule "NAME_YYYYMMDD_levels#". My goal is to write a loop that would start at a specified date and import all files up to some other date (in most cases, only the date would change). I'm guessing what I need is to open the first file with with WFOPEN and then use IMPORT to get the rest of the data into the same workfile but I don't have a clue how to incorporate the loop.
Further, I'd like the loop to automatically name the (comma-separated) columns of the data, according to the rule "A1 A2 A3 A4 B1 B2 B3 B4" etc., i.e. repeat the names of the column every fourth time and numbering them accordingly, up to the number of levels specified in the file name ("levels#" above) - is this possible?
Any thoughts on this would be greatly appreciated.
Thanks,
Nils
I am trying to get EViews to import multiple files into the same workfile using a loop that accounts for changing filenames and dynamically names colums, if possible.
I have files with automatically created filenames according to the rule "NAME_YYYYMMDD_levels#". My goal is to write a loop that would start at a specified date and import all files up to some other date (in most cases, only the date would change). I'm guessing what I need is to open the first file with with WFOPEN and then use IMPORT to get the rest of the data into the same workfile but I don't have a clue how to incorporate the loop.
Further, I'd like the loop to automatically name the (comma-separated) columns of the data, according to the rule "A1 A2 A3 A4 B1 B2 B3 B4" etc., i.e. repeat the names of the column every fourth time and numbering them accordingly, up to the number of levels specified in the file name ("levels#" above) - is this possible?
Any thoughts on this would be greatly appreciated.
Thanks,
Nils
-
EViews Gareth
- Fe ddaethom, fe welon, fe amcangyfrifon
- Posts: 13604
- Joined: Tue Sep 16, 2008 5:38 pm
Re: Import multiple files automatically
If the files are all in the same folder on your disk (and are the only things in that folder), then perhaps the best way to loop through them would be by using the @wdir function to return a list of all files in the folder, then looping through them. Something like:
You can capture the number of levels from the %file variable. I'm not 100% certain I understand the format, but something like:
should do it.
Once you have the number of levels, you can build up the list of new names with something like:
You can then loop through the %names list, renaming the imported series to the names.
Code: Select all
'get list of files
%filenames = @wdir("c:\temp\mydocs")
'change working directory to the folder
cd "c:\temp\mydocs"
'open the first file
%file = @word(%filenames,1)
wfopen %file
'loop through the remaining files, importing them one at a time
for !i=2 to @wcount(%filenames)
%file = @word(%filenames, !i)
import %file
next
Code: Select all
!levels = @val(@right(%file, 1))
Once you have the number of levels, you can build up the list of new names with something like:
Code: Select all
for %s "a b"
for !i=1 to 4
%names = %names + %s + @str(!i) + " "
next
next
Re: Import multiple files automatically
Hi Gareth,
thanks a lot for your help, this seems to work perfectly. However, it is a bit of a black box to me and I don't fully understand what EViews is doing here exactly. I can't find any info on WDIR or WCOUNT in the command reference, is there any further information on those commands?
thanks a lot for your help, this seems to work perfectly. However, it is a bit of a black box to me and I don't fully understand what EViews is doing here exactly. I can't find any info on WDIR or WCOUNT in the command reference, is there any further information on those commands?
-
EViews Gareth
- Fe ddaethom, fe welon, fe amcangyfrifon
- Posts: 13604
- Joined: Tue Sep 16, 2008 5:38 pm
Re: Import multiple files automatically
They're listed in the Command Reference (open up the PDF version and do a search for each of them).
However, @wdir just returns a space delimited list of files in a directory. @wcount returns the number of space delimited items in a list.
However, @wdir just returns a space delimited list of files in a directory. @wcount returns the number of space delimited items in a list.
Re: Import multiple files automatically
Hi Gareth,
I can't find the command reference for EViews 7 online, only the ones for 5 and 6, but it appears that the functions you used are not included in 5 and 6 (I'm using 7.1 myself) - any idea where to find that?
I can't find the command reference for EViews 7 online, only the ones for 5 and 6, but it appears that the functions you used are not included in 5 and 6 (I'm using 7.1 myself) - any idea where to find that?
-
EViews Gareth
- Fe ddaethom, fe welon, fe amcangyfrifon
- Posts: 13604
- Joined: Tue Sep 16, 2008 5:38 pm
Re: Import multiple files automatically
The command reference for EV7 should be available from the help menu in EV7.
Re: Import multiple files automatically
Hi Gareth,
thanks again - I am using EViews on university computers and apparently not every one of them has the command reference installed but I found one now.
The program is running now and doing what I want it to, with one minor flaw: It seems to read the files in an arbitrary order instead of day-by-day. As the date is included in the filename in the "YYYYMMDD" format, there should be an easy way out by just ordering the arguments that go into the filename variable alphabetically, but in the command reference I can only find commands sorting the workfile by some series, not values within a variable. Any idea how this could be done?
thanks again - I am using EViews on university computers and apparently not every one of them has the command reference installed but I found one now.
The program is running now and doing what I want it to, with one minor flaw: It seems to read the files in an arbitrary order instead of day-by-day. As the date is included in the filename in the "YYYYMMDD" format, there should be an easy way out by just ordering the arguments that go into the filename variable alphabetically, but in the command reference I can only find commands sorting the workfile by some series, not values within a variable. Any idea how this could be done?
-
EViews Gareth
- Fe ddaethom, fe welon, fe amcangyfrifon
- Posts: 13604
- Joined: Tue Sep 16, 2008 5:38 pm
Re: Import multiple files automatically
You can use @wsort to sort the string variable containing the file names. Although I believe it is already sorted.
Re: Import multiple files automatically
Hi Gareth,
an additional question: I am trying to extend the code to import similar files into a separate workfile page (as EViews does when I manually select "Create new page" in Step 4 of the import dialogue window), but I can't find the command for this in the command reference, only the other three options are discussed. Any idea what this command could be?
an additional question: I am trying to extend the code to import similar files into a separate workfile page (as EViews does when I manually select "Create new page" in Step 4 of the import dialogue window), but I can't find the command for this in the command reference, only the other three options are discussed. Any idea what this command could be?
-
EViews Gareth
- Fe ddaethom, fe welon, fe amcangyfrifon
- Posts: 13604
- Joined: Tue Sep 16, 2008 5:38 pm
Re: Import multiple files automatically
pageload?
Re: Import multiple files automatically
Hi Gareth,
I have another small issue that I can't get my head around: I built up the list of names as follows:
but when trying to rename the existing series with the names on that list with this:
I get the "@word(%names, 1) is an illegal or reserved name, even though by just using the command "@word(%names, 1)" I can generate the output "ask_price_1" - any idea why EViews won't accept that as the new name for the series?
I have another small issue that I can't get my head around: I built up the list of names as follows:
Code: Select all
'build up list of names
for %s "ask_price_" "ask_size" "bid_price_" "bid_size_ "
for !i=1 to !levels
%names = %names + %s + @str(!i) + " "
next
nextCode: Select all
string names=%names
'rename series
for !i=1 to 9
rename series0!i @word(%names, !i)
next-
EViews Gareth
- Fe ddaethom, fe welon, fe amcangyfrifon
- Posts: 13604
- Joined: Tue Sep 16, 2008 5:38 pm
Re: Import multiple files automatically
@word returns a string. You can't use a string (i.e. a piece of text surrounded by quotes) as a variable name.
Code: Select all
'rename series
for !i=1 to 9
%name = @word(%names, !i)
rename series0!i {%name}
next
Who is online
Users browsing this forum: No registered users and 2 guests
