BookmarkSubscribeRSS Feed

[HASH Object] HASH 테이블을 사용하여서 BY 그룹 내의 극단치 제거하기

Started ‎06-12-2020 by
Modified ‎06-12-2020 by
Views 101

* 제목 : HASH 테이블을 사용하여서 BY 그룹 내의 극단치 제거하기.;

 

* Sample 25990: Use a hash table to remove the top and bottom 10% of data within a BY-Group;

 

* 출처 : http://support.sas.com/kb/25/990.html;

 

* 목적 : HASH 테이블을 사용하여서 BY 그룹내에서 데이터 상/하위 10% 제거하기.

         ( Support의 예제 프로그램이 문제가 있어서 수정함. );

 

data test;

     input i j;

datalines;

1 .08

1 2

1 7

1 9

1 12

1 13

1 22

1 14

1 12.2

1 12.1

1 .09

1 2.1

1 7.1

1 9.1

1 12.12

1 13.1

1 22.1

1 14.1

1 12.3

1 12.4

2 100

2 10

2 11

2 12

2 9

2 101

2 10.1

2 11.1

2 12.1

2 9.1

;

 

PROC SORT DATA=test;

     BY I J;

RUN;

 

data final;

     /* TEST 데이터 세트의 관측치가 로딩 될 HASH 테이블에 대한 선언 및 */

     /* 인스턴트화한다. 데이터는 오름차순으로 저장된다. */

     if _n_=1 then do;

       declare hash h(ordered:'a');

       declare hiter hit('h');

       h.definekey('i','j');

       h.definedata('i','j');

       h.definedone();

     end;

     /* TEST 데이터 세트의 관측치를 BY 그룹별로 HASH 테이블 H에 로딩한다. */

     do until (last.i);

        set test;

            by i j;

            rca=h.add();

            * 확인용 : h.output(dataset: "work.out");

     end;

 

     /* 현재 작업 대상 BY 그룹 내의 관측치 개수와 삭제하기 위한 관측치의 개수(양쪽 극단치 10%)를 계산한다. */

     byct=h.num_items;

     obs2del=round((byct-byct * .8)/2);

     /* HASH 테이블에서 반복 작업을 수행한다. 양쪽 극단치를 제외한 관측치의 80%를 출력한다. */

     rcf=hit.first();

     do x=1 to byct;

        if x gt obs2del and x le byct-obs2del then output;

        rch=hit.next();

     end;

 

     /* 관측치 개수와 제거될 관측치의 개수를 계산하기 위하여 GROUP BY 의

        그룹에서 데이터 정리를 마친 그룹을 HASH 테이블에서 제거한다. */

 

     /* 방법1 : HASH 테이블을 메모리에서 정리 작업을 수행한다. */

     rc = h.clear();

 

     /* 방법2 : HASH 테이블을 메모리에서 정리 작업을 수행한다. */

     /*

     do while (hit.next() = 0);

        rc=h.remove(key:_I,key:_j);

        _I=I;

        _j=j;

     end;

     rc=h.remove(key:_I,key:_j); 

     */

 

     /* 방법3 : HASH 테이블을 정리한다. */

      /*

     CHK=0;     

     do while (CHK = 0);

        PUT _ALL_;

        _i=i;

        _j=j;

        CHK = hit.next();

        rc=h.remove(key:_i,key:_j);

     end;

     */

 

     /* 방법4 : HASH 테이블을 정리한다. */

     /*

     do UNTIL (LAST.I);

        _i=i;

        _j=j;

        LAST.I  = hit.next();

        rc=h.remove(key:_i,key:_j);

     end;

     */

     keep i j; 

run;

 

proc print;

run;

 

 

***********************************************************
- 통계분석연구회 
- 카페 : http://cafe.daum.net/statsas
- 통계분석연구회 페이지 : https://www.facebook.com/statsas 

- 통계분석연구회(Statistics Analysis Study) 그룹 : https://www.facebook.com/groups/statsas/ 

Version history
Last update:
‎06-12-2020 05:24 AM
Updated by:
Contributors

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Article Labels
Article Tags