Tracking different samples with if statement

For questions regarding programming in the EViews programming language.

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

maragloria
Posts: 105
Joined: Tue Jun 29, 2010 7:09 am

Tracking different samples with if statement

Postby maragloria » Thu Mar 23, 2017 5:43 am

Hi there,

I wrote the following code to assign different values to matrix temp in different samples.
I would like some suggestions to simplify the if statement below:

Code: Select all

%sample1 = "1999q1 2001q4" %sample2 = "2002q1 2004q4" for %1 %2 {%sample1} {%sample2} smpl {%1} {%2} %currsmpl = @pagesmpl 'get current sample string ss0 = @word(%currsmpl,1) 'only 1st element of sample is enough string ss1 = @word(%sample1,1) string ss2 = @word(%sample2,1) if @eqna(%ss0,%ss1) then matrix temp = mat01 else if @eqna(%ss0,%ss2) then matrix temp = mat02 endif endif next
Could I make the if statement simpler? Something like:
if %currsmpl = %sample1 then ...

I've tried different versions but to no avail:
if {%currsmpl} = {%sample1} then ...
if @eqna(%currsmpl,%sample1) then ...

Many thanks!

maragloria
Posts: 105
Joined: Tue Jun 29, 2010 7:09 am

Re: Tracking different samples with if statement

Postby maragloria » Thu Mar 23, 2017 8:22 am

Actually, the code I posted is not doing what I intend. Your help would be really appreciated. Thanks!

EViews Matt
EViews Developer
Posts: 583
Joined: Thu Apr 25, 2013 7:48 pm

Re: Tracking different samples with if statement

Postby EViews Matt » Thu Mar 23, 2017 9:35 am

Hello,

The immediate issue with your code is that you've created string objects (ss0, ss1, ss2) but later used string variables (%ss0, %ss1, %ss2). Those non-existant variables will be treated as NAs by @eqna, thus your first if statement conditional will always be true and temp will always be assigned mat01.

After fixing that issue, the next issue you'll find is that @pagesmpl returns an upper-case string while your sample strings are lower-case, thus none of your if statement conditionals will be true. That's easy to fix by changing the q's in your samples to Q's, or by using @upper elsewhere in your code.

However, regarding simplifying your code, there are some ways you could completely eliminate the if statements. If you'll always iterate through your samples in a consistent order you can make the assignment to temp dependent on an iteration number rather than the current sample. For example,

Code: Select all

%sample1 = "1999Q1 2001q4" %sample2 = "2002Q1 2004q4" !i = 1 for %1 %2 {%sample1} {%sample2} %tmp = @str(!i, "i02") matrix temp = mat{%tmp} !i = !i + 1 next
The above code assumes that the matrices you're assigning from are regularly named (mat01, mat02, etc.), but you can just as easily use a list of arbitrary matrix names. For example,

Code: Select all

%sample1 = "1999Q1 2001q4" %sample2 = "2002Q1 2004q4" %mats = "mat01 mat02" !i = 1 for %1 %2 {%sample1} {%sample2} %tmp = @word(%mats, !i) matrix temp = {%tmp} !i = !i + 1 next
There may be other, better ways to organize the for loop based on the work your program will perform, but the above are some starting points.

maragloria
Posts: 105
Joined: Tue Jun 29, 2010 7:09 am

Re: Tracking different samples with if statement

Postby maragloria » Thu Mar 23, 2017 10:30 am

This is great! Thanks for all your suggestions.


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests