Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculation within a loop

Status
Not open for further replies.

mikeymay

Programmer
May 2, 2007
10
GB
I have a procedure that loops through 2 sets of arrays and creates another by performing a calculation -
data New_Reg_Meas_Vol_Workload (drop=T_ACL--SL_WTG_BB SL_WTG_VOL--SL_FWO_VOL);
merge lookup.Unit_Times_Transpose(in=UnitTimes) New_Reg_Meas_Vol(in=MeasVols);
by Jurisdiction;
if UnitTimes and MeasVols;

array MeasVol (5) SL_WTG_VOL SL_NAO_VOL SL_ITV_VOL SL_FWC_VOL SL_FWO_VOL;
array UnitTime (5) SL_WTG SL_NAO SL_ITV SL_FWC SL_FWO;
array MeasWld (5) SL_WTG_WLD SL_NAO_WLD SL_ITV_WLD SL_FWC_WLD SL_FWO_WLD;

do i=1 to 5;

if MeasVol(i) = 0 then MeasWld (i) = 0;
else MeasWld (i) = (MeasVol(i)* UnitTime(i))/60;


end;
drop i;
output;

run;

For one of the variables I would like to perform a different calculation to the one referenced in the loop but I'm not sure if this needs to be done within the or can be done outside of it.


Thanks in advance.
 
Hi Mickey,

The short answer is it can be done both ways, but if you are declaring something explicitly - such as the array vectors - then it is best done outside of the loop to minimise the number of times the code is executed.

HTH

Code:
data mickey;
input val1-val6;
cards;
1 2 3 4 5 6
7 8 9 10 11 12
;
data mickey;
   set mickey;
   array test(*)val1-val6;
   * Multiply all values in array by 2;
   do i=1 to dim(test);
      test[i] = test[i]*2;
      end;
   * If val1 is 2 then multiply it by 3;
   if test[1] = 2 then test[1] = test[1] * 3;
proc print;run;
 
As a newbie with SAS I can sort of see what that codes does but did manage to resolve the issue by placing another if statement just after the first one. Performs the different calculation if i=4 and seems to do the trick!!

Thanks for your help though :eek:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top