Вы описываете fare
как двумерный массив. То, как вы «загружаете» массив, зависит от того, как вы планируете использовать «поиск».
Предположим:
- тарифов в одном наборе данных:
station_code
, dest_code
, fare
- значения кода станции буквально
station1
… station91
- точек маршрута во втором наборе данных:
personid
, step_num
, station_code
- Вы хотите рассчитать общий тариф для каждого человека
* +1025 * Пример:
data totals(keep=personid totalfare);
* load the station fares into temporary array for use as lookup table;
array fares(91,91) _temporary_;
do until (lastfare);
set fares end=lastfare;
_from = input(substr(station_code,8),best.); * parse the number out of code;
_dest = input(substr(dest_code,8),best.);
fares(_from,_dest) = fare;
end;
* compute each persons total fare;
do until (endtrips);
totalfare = 0;
_from = 0;
do until (last.personid);
set trips end=endtrips;
by personid step_num;
_dest = input(substr(station_code,8),best.);
if _from and _dest then totalfare + fares(_from,_dest);
_from = _dest;
end;
output;
end;
stop;
run;
Если значения кода станции на самом деле , а не значение, из которого можно проанализировать 1
… 91
, массив не может использоваться - Вместо этого хеш-объект с двухзначным ключом следует использовать в качестве поиска.
data totals (keep=personid totalfare);
* load the station fares into hash for use as lookup table;
if 0 then set fares; * prep pdv;
declare fares hash(dataset:'fares');
fares.defineKey('dest_code', 'station_code'); * reversed keys make it easier to traverse trips;
fares.defineData('fare');
fares.defineDone(); * automatically reads dataset:fares and fills hash entries;
* compute each persons total fare;
do until (endtrips);
totalfare = 0;
dest_code = '';
do until (last.personid);
set trips end=endtrips; * read in the station for a persons step;
by personid step_num;
if fares.find()=0 then do; * 0 return code means variable fare has the value for the fare from station_code to dest_code;
totalfare + fare;
end;
* prepare for next leg of journey, this is what is meant by easier to traverse;
dest_code = station_code;
end;
output;
end;
stop;
run;