Looping over a string list when importing csvs

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt

pofter28
Posts: 2
Joined: Mon Mar 22, 2021 3:23 pm

Looping over a string list when importing csvs

Postby pofter28 » Mon Mar 22, 2021 3:35 pm

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!

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13307
Joined: Tue Sep 16, 2008 5:38 pm

Re: Looping over a string list when importing csvs

Postby EViews Gareth » Mon Mar 22, 2021 4:13 pm

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.
Follow us on Twitter @IHSEViews

pofter28
Posts: 2
Joined: Mon Mar 22, 2021 3:23 pm

Re: Looping over a string list when importing csvs

Postby pofter28 » Tue Mar 23, 2021 1:29 am

EViews Gareth wrote: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?

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13307
Joined: Tue Sep 16, 2008 5:38 pm

Re: Looping over a string list when importing csvs

Postby EViews Gareth » Tue Mar 23, 2021 9:22 am

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
Follow us on Twitter @IHSEViews


Return to “Programming”

Who is online

Users browsing this forum: Google [Bot] and 31 guests