如何使用数组创建查找表

问题描述 投票:0回答:3

我有一个包含三列的数据集:station_code,dest_code,fare。基本上,station_code和dest_cde内的数据是相同的,票价是去特定电台所需的费用。

station_code dest_code fare
station1     station1  0
station1     station2  4.6
station1     station3  10
station1     station4  10
station1     station5  12.3
station1     station6  12.3
station1     station7  12.3
station1     station8  12.3
station1     station9  14.7 
station1     station10 14.7
           .
           .
           .
station1     station91 27.5
station2     station1  4.6
station2     station2  0
station2     station3  10
station2     station4  10
station2     station5  12.3
station2     station6  12.3
station2     station7  12.3
station2     station8  12.3
station2     station9  14.7 
station2     station10 14.7
          .
          .
          .

到station91

所以我的问题是如何使用数组技术创建一个看起来像这样的查找表。

fee  1    2    3    4   ...
1    0    4.6  10   10
2    4.6  0    10   10
3    10   10   0    4.6
4    10   10   4.6  0
5    12.3 12.3 4.6  4.6
...  ...  ...  ...  ...

如您所见,行和列中的索引实际上代表了站名,例如row1 = station1,column1 = station1,column2 = station2。

sas sas-macro
3个回答
0
投票

问题是如此混乱,我想再次澄清我的表和查找表。

对于第一张图片,它是原始表enter image description here

enter image description here第二张图片是我想要的查找表


0
投票

您可以通过PROC TRANSPOSE获得这样的表格,提前对数据进行排序。

 proc sort data=have ; 
 by station_code dest_code fare;
 run;

 proc transpose data=have out=want;
 by station_code;
 id dest_code;
 var fare;
 run;

0
投票

您将fare描述为二维数组。如何“加载”数组取决于您计划如何使用“查找”。

假设:

  • 一个数据集中的票价:station_codedest_codefare 站代码值字面上是station1 ... station91
  • 行程点在第二个数据集:personidstep_numstation_code
  • 你想计算每个人的总票价

例:

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;
© www.soinside.com 2019 - 2024. All rights reserved.