Page 1 of 1

Looping over a string list when importing csvs

Posted: Mon Mar 22, 2021 3:35 pm
by pofter28
I would like to define a list of strings, say,

Code: Select all

countries = C1, C2, C3, ... , C10
where I would use each element of the set to import differently named .csv, sort of like this,

Code: Select all

import "my_path\my_file_C1.csv" ftype=ascii rectype=crlf skip=0 fieldtype=delimited delim=comma colhead=1 eoltype=pad badfield=NA @freq M @id id @date(date) @smpl @all
and then run a few lines of code before exporting,

Code: Select all

wfsave(type=excelxml) "my_path\results\results_C1.csv"
and then move onto the next country, C2, in a sequence and perform exactly the same codes up until country C10.

How can I achieve this with a for loop so that its possible to loop over my defined list of string names?

In addition, what are the commands to time this loop to see how long it took Eviews to execute the program?

Help is appreciated!

Re: Looping over a string list when importing csvs

Posted: Mon Mar 22, 2021 4:13 pm
by EViews Gareth
Read through the programming training slides:
http://eviews.com/Learning/programming_a.html

Particularly replacement variables and for loops.

The tic and @toc commands can be used for timing.

Re: Looping over a string list when importing csvs

Posted: Tue Mar 23, 2021 1:29 am
by pofter28
Read through the programming training slides:
http://eviews.com/Learning/programming_a.html

Particularly replacement variables and for loops.

The tic and @toc commands can be used for timing.
neither this works

Code: Select all

for %countries C1 C2 C3 import "my_path\my_file_{%countries}.csv" ftype=ascii rectype=crlf skip=0 fieldtype=delimited delim=comma colhead=1 eoltype=pad badfield=NA @freq M @id id @date(date) @smpl @all next
nor this

Code: Select all

for %countries C1 C2 C3 import "my_path\my_file_%countries.csv" ftype=ascii rectype=crlf skip=0 fieldtype=delimited delim=comma colhead=1 eoltype=pad badfield=NA @freq M @id id @date(date) @smpl @all next
nor this

Code: Select all

for %countries "C1" "C2" "C3" import "my_path\my_file_{%countries}.csv" ftype=ascii rectype=crlf skip=0 fieldtype=delimited delim=comma colhead=1 eoltype=pad badfield=NA @freq M @id id @date(date) @smpl @all next
nor this

Code: Select all

%countries = "C1 C2 C3" for %c {%countries} import "my_path\my_file_{%c}.csv" ftype=ascii rectype=crlf skip=0 fieldtype=delimited delim=comma colhead=1 eoltype=pad badfield=NA @freq M @id id @date(date) @smpl @all next
All of them return an error whereby Eviews does not recognize that {%C1} should mean C1. If I replace in code {%countries}, % countries or {%c} with actual country name,C1, then it will import the csv otherwise it will not. What is missing here?

Re: Looping over a string list when importing csvs

Posted: Tue Mar 23, 2021 9:22 am
by EViews Gareth
Replacement doesn't occur within quotes.

Code: Select all

for %countries C1 C2 C3 %filename = "my_path\my_file_" + %countries + ".csv" import %filename ftype=ascii rectype=crlf skip=0 fieldtype=delimited delim=comma colhead=1 eoltype=pad badfield=NA @freq M @id id @date(date) @smpl @all next