BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I am using SELECT and WHEN to check values in a loop.

I am having a problem with the following code -

do i=1 to 5;

if Rate(i)=0 then
UnmeasVol(i) = 0;
else
select (i);
when (4)
if ExSuccVol + ExUnsuccVol + NewSuccVol + NewUnsuccVol = 0 then UnmeasVol(4) = 0;
else
UnmeasVol (4) = TotMeasWld / (ExSuccVol + ExUnsuccVol + NewSuccVol+ NewUnsuccVol) * (ExBookedConv + NewBookedConv)*Rate(i);
when (5)
UnmeasVol(5) = Ex_Reg_Meas_Workload.SL_NAO_WLD + New_Reg_Meas_Workload.SL_NAO_WLD;
Otherwise
UnmeasVol(i)=TotMeasRegWld * Rate(i);
end;
end;

It seems to fall down when trying to execute the code after When (4) (i=4) and is expecting WHEN, OTHERWISE or END.

Not sure why.........any ides????
3 REPLIES 3
GertNissen
Barite | Level 11
Your syntax is wrong - try one of the 2 altenatives (must be modified for your need)
1)[pre]
do i=1 to 5;
if Rate(i)=0 then
UnmeasVol(i) = 0;
else
select (i);
when (4)
UnmeasVol(4) = TotMeasWld / (ExSuccVol + ExUnsuccVol + NewSuccVol+ NewUnsuccVol) * (ExBookedConv + NewBookedConv)*Rate(i);
when (5)
UnmeasVol(5) = Ex_Reg_Meas_Workload.SL_NAO_WLD + New_Reg_Meas_Workload.SL_NAO_WLD;
Otherwise
UnmeasVol(i)=TotMeasRegWld * Rate(i);
end;
if ExSuccVol + ExUnsuccVol + NewSuccVol + NewUnsuccVol = 0 then UnmeasVol(4) = 0;
end;
[/pre]
2)[pre]
do i=1 to 5;
if Rate(i)=0 then
UnmeasVol(i) = 0;
else
select (i);
when (4)
do;
if ExSuccVol + ExUnsuccVol + NewSuccVol + NewUnsuccVol = 0 then UnmeasVol(4) = 0;
else
UnmeasVol (4) = TotMeasWld / (ExSuccVol + ExUnsuccVol + NewSuccVol+ NewUnsuccVol) * (ExBookedConv + NewBookedConv)*Rate(i);
end;
when (5)
UnmeasVol(5) = Ex_Reg_Meas_Workload.SL_NAO_WLD + New_Reg_Meas_Workload.SL_NAO_WLD;
Otherwise
UnmeasVol(i)=TotMeasRegWld * Rate(i);
end;
end;
[/pre]
More info at http://support.sas.com/onlinedoc/913/getDoc/da/lrdict.hlp/a000201966.htm
deleted_user
Not applicable
Ok, that makes sense, but it does seem strange that you can't perform an IF statement within a SELECT WHEN statement?

Is there any other way of performing such a function within a SELECT WHEN statement as I can forsee me having problems in future if I can't do this?


Thanks for the code though 🐵
GertNissen
Barite | Level 11
You can use IF within as SELECT statement - Look at http://support.sas.com/onlinedoc/913/getDoc/da/lrdict.hlp/a000201966.htm#a000202177 or my example 2) above.

Your problem is not the IF statement, but the fact that your are using more than 1 line of code within the WHEN statement. You must use DO/END structure, to write more statements i.e. IF/THEN/ELSE.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 756 views
  • 0 likes
  • 2 in conversation