Page 1 of 1

Case-switch

Posted: Mon May 04, 2015 10:24 am
by EconMike
Hi there,

Just wondering what would be the easiest way to implement something akin to a switch-case statement in EViews. Is there some sort of dictionary or map function, or are nested if-statements the best option as in the code below?

Thanks in advance,

Michael

for %ind CN RL AU
if %ind="CN" then
%naics = "23"
else
if %ind="RL" then
%naics = "53"
else
if %ind="AU" then
%naics = "71"
endif
endif
endif
...
...
next

Re: Case-switch

Posted: Mon May 04, 2015 10:27 am
by EViews Gareth
Nested ifs :(

Re: Case-switch

Posted: Mon May 04, 2015 10:31 am
by EconMike
:( indeed. It could get pretty ugly.

Thanks anyway.

Re: Case-switch

Posted: Mon May 04, 2015 11:28 am
by EViews Glenn
You can fake a dictionary using something like

Code: Select all

%list1 = "cn rl au gb fc" svector a = @wsplit(%list1) %list2 = "23 53 71 53 94" svector b = @wsplit(%list2) for %z cn rl au for !i=1 to @rows(a) if (%z = a(!i)) then %naics = b(!i) %temp = %z + ": " + %naics statusline %temp break endif next next
Here I'm using %temp to show the paired strings at the bottom of the screen.

If you need to do this a lot, I'd make a subroutine for the inner matching loop and pass in the comparison and lookup vectors. Then you'd have something like

Code: Select all

%list1 = "cn rl au gb fc" svector a = @wsplit(%list1) %list2 = "23 53 71 53 94" svector b = @wsplit(%list2) for %z cn rl au call lookup(%naics, a, b) next
where the subroutine lookup is defined appropriately.

Re: Case-switch

Posted: Mon May 04, 2015 12:30 pm
by EconMike
Thanks for that Glenn. This should do the trick, and the subroutine makes it far cleaner.

Cheers,

Michael