Page 1 of 1

Import multiple files automatically

Posted: Tue Jun 14, 2011 8:52 am
by nilshg13
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

Re: Import multiple files automatically

Posted: Tue Jun 14, 2011 9:08 am
by EViews Gareth
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:

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
You can capture the number of levels from the %file variable. I'm not 100% certain I understand the format, but something like:

Code: Select all

!levels = @val(@right(%file, 1))
should do it.

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
You can then loop through the %names list, renaming the imported series to the names.

Re: Import multiple files automatically

Posted: Thu Jun 16, 2011 4:20 am
by nilshg13
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?

Re: Import multiple files automatically

Posted: Thu Jun 16, 2011 8:22 am
by EViews Gareth
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.

Re: Import multiple files automatically

Posted: Tue Jun 21, 2011 9:44 am
by nilshg13
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?

Re: Import multiple files automatically

Posted: Tue Jun 21, 2011 9:46 am
by EViews Gareth
The command reference for EV7 should be available from the help menu in EV7.

Re: Import multiple files automatically

Posted: Thu Jun 23, 2011 5:27 am
by nilshg13
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?

Re: Import multiple files automatically

Posted: Thu Jun 23, 2011 7:55 am
by EViews Gareth
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

Posted: Tue Jul 05, 2011 7:13 am
by nilshg13
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?

Re: Import multiple files automatically

Posted: Tue Jul 05, 2011 8:46 am
by EViews Gareth
pageload?

Re: Import multiple files automatically

Posted: Thu Jul 28, 2011 10:04 am
by nilshg13
Hi Gareth,

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 next
but when trying to rename the existing series with the names on that list with this:

Code: Select all

string names=%names 'rename series for !i=1 to 9 rename series0!i @word(%names, !i) next
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?

Re: Import multiple files automatically

Posted: Thu Jul 28, 2011 10:38 am
by EViews Gareth
@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