1

コードの重複を避けるのに役立つ、以下の問題に対するエレガントな解決策を探しています。次の行が表示されます。

put auction_id= potential_buyer= ;* THIS GETS REPEATED;

このコードで繰り返されます:

data results;

  attrib potential_buyer length=$1;

  set auction;

  if _n_ eq 1 then do;
    declare hash ht1(dataset:'buyers', multidata: 'y');
    ht1.definekey('auction_id');
    ht1.definedata('potential_buyer');
    ht1.definedone();
    call missing (potential_buyer);
  end;


  **
  ** LOOP THROUGH EACH POTENTIAL BUYER AND PROCESS THEM
  *;
  if ht1.find() eq 0 then do;

    put auction_id= potential_buyer= ;* THIS GETS REPEATED;

    ht1.has_next(result: ht1_has_more);
    do while(ht1_has_more);
      rc = ht1.find_next();

      put auction_id= potential_buyer= ;* THIS GETS REPEATED;

      ht1.has_next(result: ht1_has_more);
    end;
  end;
run;

実際のコード ブロックは非常に長く複雑であるため、上記の例を 1 行に簡略化しました。ロジックをデータステップの「内」に保持したいので%macro、可能であればスニペットまたはを使用することは避けたいと思います。%include

サンプルデータは次のとおりです。

    data auction;
      input auction_id;
    datalines;
    111
    222
    333
    ;
    run;

    data buyers;
      input auction_id potential_buyer $;
    datalines;
    111 a
    111 c
    222 a
    222 b
    222 c
    333 d
    ;
    run;
4

2 に答える 2

3

私はそれを考え出した。最終的には非常に単純であることが判明しましたが、私の頭脳を包み込むのに少し苦労しました。

data results;

  attrib potential_buyer length=$1;

  set auction;

  if _n_ eq 1 then do;
    declare hash ht1(dataset:'buyers', multidata: 'y');
    ht1.definekey('auction_id');
    ht1.definedata('potential_buyer');
    ht1.definedone();
    call missing (potential_buyer);
  end;


  **
  ** LOOP THROUGH EACH POTENTIAL BUYER AND PROCESS THEM
  *;
  if ht1.find() eq 0 then do;

    keep_processing = 1;
    do while(keep_processing);

      put auction_id= potential_buyer= ;* THIS GETS DOESNT GET REPEATED ANYMORE =);

      ht1.has_next(result: keep_processing);
      rc = ht1.find_next();
    end;
  end;

run;
于 2013-02-22T18:58:03.337 に答える
1

この方法で解決できます....しかし、ロブの答えの方が優れています。

data results;

 %Macro NoDuplicate;
  Put auction_id= potential_buyer= ; * No Longer Duplicated;
 %Mend noduplicate;

 attrib potential_buyer length=$1;

 set auction;

 if _n_ eq 1 then do;
  declare hash ht1(dataset:'buyers', multidata: 'y');
  ht1.definekey('auction_id');
  ht1.definedata('potential_buyer');
  ht1.definedone();
  call missing (potential_buyer);
 end;

 **
 ** LOOP THROUGH EACH POTENTIAL BUYER AND PROCESS THEM
 *;
 if ht1.find() eq 0 then do;

  %NoDuplicate

  ht1.has_next(result: ht1_has_more);
  do while(ht1_has_more);
   rc = ht1.find_next();
   %NoDuplicate
   ht1.has_next(result: ht1_has_more);
  end;
 end;

run;
于 2013-02-22T18:54:33.460 に答える