Case-switch

For questions regarding programming in the EViews programming language.

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

EconMike
Posts: 5
Joined: Mon May 04, 2015 9:57 am

Case-switch

Postby EconMike » Mon May 04, 2015 10:24 am

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

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

Re: Case-switch

Postby EViews Gareth » Mon May 04, 2015 10:27 am

Nested ifs :(

EconMike
Posts: 5
Joined: Mon May 04, 2015 9:57 am

Re: Case-switch

Postby EconMike » Mon May 04, 2015 10:31 am

:( indeed. It could get pretty ugly.

Thanks anyway.

EViews Glenn
EViews Developer
Posts: 2682
Joined: Wed Oct 15, 2008 9:17 am

Re: Case-switch

Postby EViews Glenn » Mon May 04, 2015 11:28 am

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.

EconMike
Posts: 5
Joined: Mon May 04, 2015 9:57 am

Re: Case-switch

Postby EconMike » Mon May 04, 2015 12:30 pm

Thanks for that Glenn. This should do the trick, and the subroutine makes it far cleaner.

Cheers,

Michael


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests