multiple repetitive if statements

For questions regarding programming in the EViews programming language.

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

Jonathan
Posts: 5
Joined: Mon Sep 14, 2015 12:45 pm

multiple repetitive if statements

Postby Jonathan » Fri Sep 18, 2015 3:54 pm

Hi,
Is there a clean way to do something like the following:

if %mif %m_long="January" then
%m_short = "01"
else
if %m_long="February" then
%m_short = "02"
else
if %m_long="March" then
%m_short = "03"
.
.
.
endif
endif
endif

That works but it's really clunky, especially for states or MSAs or any list of more than three or four. Thanks!
Jonathan

startz
Non-normality and collinearity are NOT problems!
Posts: 3798
Joined: Wed Sep 17, 2008 2:25 pm

Re: multiple repetitive if statements

Postby startz » Fri Sep 18, 2015 4:11 pm

Hi,
Is there a clean way to do something like the following:

if %mif %m_long="January" then
%m_short = "01"
else
if %m_long="February" then
%m_short = "02"
else
if %m_long="March" then
%m_short = "03"
.
.
.
endif

endif
endif

That works but it's really clunky, especially for states or MSAs or any list of more than three or four. Thanks!
Jonathan

Code: Select all

%m_short = @recode(%m_long="January","01",%m_short) %m_short = @recode(%m_long="February","02",%m_short)
or something like that

Jonathan
Posts: 5
Joined: Mon Sep 14, 2015 12:45 pm

Re: multiple repetitive if statements

Postby Jonathan » Fri Sep 18, 2015 4:32 pm

Okay, I guess looping through a table of paired values is best for a long list. Thanks for the fast reply!
Jonathan

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

Re: multiple repetitive if statements

Postby EViews Gareth » Fri Sep 18, 2015 4:42 pm

Create an svector with the short values you want in it, then just do this:

Code: Select all

svector longs = @uniquevals({%m_long}) for !i=1 to @rows(longs) %long = longs(!i) %short = shorts(!i) {%m_short} = @recode({%m_long} = %long, %short, {%m_short}) next
(where I called my short svector "shorts").

Jonathan
Posts: 5
Joined: Mon Sep 14, 2015 12:45 pm

Re: multiple repetitive if statements

Postby Jonathan » Tue Sep 22, 2015 10:15 am

Thanks for that. The use of recode is something I need to consider more.
Jonathan


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests