Hi,
I am trying to use an If statement within a For loop to apply some manipulation to certain variables only. In the If statement I have two conditions. One is that !i<>!j which seems to work. In the second condition I am breaking down the elements in the string vector sv_cc, which contains country names. For some countries, I want it to perform this [series {%prfij}= ({%prd}/{%prf})^w(!i,!j)], for others I want it to perform this other calculation [series {%prfij}= (({%prd}/{%prf})^w(!i,!j))*{%gdpw}]. However, when I run the code the if statement for the second condition does not work, and applies the first formulas to all the elements in sv_cc.
Do you know where my mistake is? Also, I am not sure whether I should use two different If statements as below or only one, linking both criteria with "and". Could you please have a look and let me know where my error is?
I am using eViews 9.
Thank you,
Marta
For !i=1 to @rows(w)
%reer= "_reer_" + sv_cc(!i)
%c = sv_cc(!i)
series {%reer}= 1
For !j=1 to @columns(w)
If !i<>!j then
If (@eqna(%c,"ca") or @eqna(%c,"uk") or @eqna(%c,"jp") or @eqna(%c,"us")) then
%prfij= "_prf_" + sv_cc(!i)+sv_cc(!j)
%prd= "_prd_" + sv_cc(!i)
%prf="_prd_" + sv_cc(!j)
series {%prfij}= ({%prd}/{%prf})^w(!i,!j)
{%reer}={%reer}*{%prfij}
else
If (@neqna(%c,"ca") or @neqna(%c,"uk") or @neqna(%c,"jp") or @neqna(%c,"us")) then
%gdpw=sv_cc(!i)+"_gdpw"
%prez="_prez_"+sv_cc(!i)
series {%prez}=0
series {%prfij}= (({%prd}/{%prf})^w(!i,!j))*{%gdpw}
series {%prez}={%prez}+{%prfij}
endif
endif
'delete {%prfij}
endif
Next
{%reer}={%reer}*{%prez}
delete {%prez}
Next
If Statement with string vector elements as conditions
Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt
-
- Fe ddaethom, fe welon, fe amcangyfrifon
- Posts: 13323
- Joined: Tue Sep 16, 2008 5:38 pm
Re: If Statement with string vector elements as conditions
Hard to see what that second if statement is trying to do.
My first guess was that you want "and" rather than "or". But if that's the case, there's no need for the if statement at all.
My first guess was that you want "and" rather than "or". But if that's the case, there's no need for the if statement at all.
Follow us on Twitter @IHSEViews
Re: If Statement with string vector elements as conditions
Thanks for you reply.
The second If is supposed to be the one dictating the calculations applied to the variables.
In any case, I tried simplifying and applying the condition to one code only as below. This finds it true for all the elements in sv_cc (see element below), and goes on with the function in this part of the If statement ignoring the else. Changing the "ca" for something random, i.e. "pp", which is not in the svector is the only way it jumps to the else condition. Any thought?
Best,
Marta
%c = @trim(sv_cc(!i))
If !i<>!j and @lower(%c)=@lower("ca") then
This is my string vector (sv_cc):
ca
uk
jp
us
de
it
fr
The second If is supposed to be the one dictating the calculations applied to the variables.
In any case, I tried simplifying and applying the condition to one code only as below. This finds it true for all the elements in sv_cc (see element below), and goes on with the function in this part of the If statement ignoring the else. Changing the "ca" for something random, i.e. "pp", which is not in the svector is the only way it jumps to the else condition. Any thought?
Best,
Marta
%c = @trim(sv_cc(!i))
If !i<>!j and @lower(%c)=@lower("ca") then
This is my string vector (sv_cc):
ca
uk
jp
us
de
it
fr
-
- Fe ddaethom, fe welon, fe amcangyfrifon
- Posts: 13323
- Joined: Tue Sep 16, 2008 5:38 pm
Re: If Statement with string vector elements as conditions
I can't see how what you say is happening can happen, so I'm going to suggest that things aren't what you think they are.
Follow us on Twitter @IHSEViews
Re: If Statement with string vector elements as conditions
Well, not sure what you mean. I have gone over and over again through the code and the data. I am obviously missing something.
I attach the workfile with the data and the program. If you run it, you will see that despite the If statement the variables _prf_ca?? all follow the formula in the first part of the if statement, [series {%prfij}= ({%prd}/{%prf})^w(!i,!j)], and _reer_ca follows accordingly.
Thanks,
Marta
I attach the workfile with the data and the program. If you run it, you will see that despite the If statement the variables _prf_ca?? all follow the formula in the first part of the if statement, [series {%prfij}= ({%prd}/{%prf})^w(!i,!j)], and _reer_ca follows accordingly.
Thanks,
Marta
- Attachments
-
- dataset.WF1
- workfile
- (285.04 KiB) Downloaded 178 times
-
- creating_variables.prg
- code
- (1.92 KiB) Downloaded 232 times
-
- Fe ddaethom, fe welon, fe amcangyfrifon
- Posts: 13323
- Joined: Tue Sep 16, 2008 5:38 pm
Re: If Statement with string vector elements as conditions
Your code is indecipherable.
Can you try to explain what you want the if statement(s) to do in English?
Can you try to explain what you want the if statement(s) to do in English?
Follow us on Twitter @IHSEViews
Re: If Statement with string vector elements as conditions
1. For CA, US, JP and the UK, I want to obtain a variable, which I call %reer, which is the geometric average of the ratio of (cpi times e) for country i and (cpi time e) for country j raised to a weight(w(i,j)).
series {%prd}=({%cpi}/100)*{%e}
series {%prfij}= ({%prd}/{%prf})^w(!i,!j)
{%reer}={%reer}*{%prfij}
2. For the rest of the countries (de, fr, it), I want to do slightly different. Same ratio than above ((cpi.e)i/(cpi.e)j) raised to the power of a weight, w(j,i), but multiplied by another variable, {%gdpw}. For these three countries, I want to add the result up
series {%prfij}= (({%prd}/{%prf})^w(!i,!j))
series {%prfijw}={%prfij}*{%gdpw}
series {%prez}={%prez}+{%prfijw}
3. Finally, I want to multiply the outcome of the sum in 2 by the outcome of the product in 1.
{%reer}={%reer}*{%prez}
series {%prd}=({%cpi}/100)*{%e}
series {%prfij}= ({%prd}/{%prf})^w(!i,!j)
{%reer}={%reer}*{%prfij}
2. For the rest of the countries (de, fr, it), I want to do slightly different. Same ratio than above ((cpi.e)i/(cpi.e)j) raised to the power of a weight, w(j,i), but multiplied by another variable, {%gdpw}. For these three countries, I want to add the result up
series {%prfij}= (({%prd}/{%prf})^w(!i,!j))
series {%prfijw}={%prfij}*{%gdpw}
series {%prez}={%prez}+{%prfijw}
3. Finally, I want to multiply the outcome of the sum in 2 by the outcome of the product in 1.
{%reer}={%reer}*{%prez}
-
- Fe ddaethom, fe welon, fe amcangyfrifon
- Posts: 13323
- Joined: Tue Sep 16, 2008 5:38 pm
Re: If Statement with string vector elements as conditions
Just a simple if statement then.
Code: Select all
if %c = "uk" or %c = "us" or %c = "ca" or %c = "jp" then
series {%prd}=({%cpi}/100)*{%e}
series {%prfij}= ({%prd}/{%prf})^w(!i,!j)
{%reer}={%reer}*{%prfij}
else
series {%prfij}= (({%prd}/{%prf})^w(!i,!j))
series {%prfijw}={%prfij}*{%gdpw}
series {%prez}={%prez}+{%prfijw}
Follow us on Twitter @IHSEViews
Who is online
Users browsing this forum: No registered users and 25 guests