Help using Base SAS procedures

IF satament in WHEN

Reply
N/A
Posts: 0

IF satament in WHEN

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????
SAS Employee
Posts: 160

Re: IF satament in WHEN

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
N/A
Posts: 0

Re: IF satament in WHEN

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 Smiley Surprised)
SAS Employee
Posts: 160

Re: IF satament in WHEN

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.
Ask a Question
Discussion stats
  • 3 replies
  • 116 views
  • 0 likes
  • 2 in conversation