BookmarkSubscribeRSS Feed

[HASH Object] HASH 테이블에서 데이터 항목의 개수 카운트하기

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

* http://cafe.daum.net/statsas/3F8j/270

 

* Sample 24594: Count the number of items in a hash table;

* 출처 : http://support.sas.com/kb/24/594.html;

 

data test;

  input key1 key2 data1;

datalines;

1 1 11

1 1 22

1 2 12

1 2 122

;

 

* HASH BOJECT는 기본적으로 KEY 값을 기준으로 중복을 허용하지 않는다.; 

 

* 중복 관측치 사용을 위한 특정 옵션을 지정하지 않고, HASH TABLE을 생성하면 HASH 테이블에는;

 

* 개별 KEY 값을 기준으로 첫번째 관측치가 로드된다.;

 

data _null_;

     if _n_ = 1 then do;

 

        * h hash object 정의;

        declare hash h(dataset: "work.test");

 

        * key 값 정의;

        h.definekey('key1','key2');

 

        * hash object에 생성되는 data 항목 정의;

        h.definedata('KEY1','KEY2','data1');

 

        * 위에서 생성한 데이터를 로딩하여 hash object 생성;

        h.definedone();

        call missing(key1, key2, data1);

     end;

 

     * SAS 데이터 세트 출력;

     rc = h.output(dataset: "work.BACK");

run;

 

 

 data nodups;

     /* 첫 번째 반복 작업을 수행하면서 HASH Object H를 선언. 

        HASH 테이블 H에 WORK.TEST 데이터를 로드.*/

     if _n_ = 1 then do;

        dcl hash h(dataset:'work.test');

        h.defineKey('key1', 'key2');

        h.definedata('data1');

        h.defineDone();

        /* "변수 'data1'이(가) 초기화되지 않았습니다." 메시지 출력을 방지하기 위하여,

           데이터 세트 변수 KEY1, KEY2 와 DATA1 변수를 결측치 값으로 초기화 */

        call missing(key1, key2, data1); 

     end;

 

     /* Assign values to the data set variables KEY1 and KEY2 */

     key1=1;

     key2=1;

 

     /* FIND 매서드를 사용하여서 HASH Object H에서 일치하는 KYE1과 KEY2을 찾는다.(KEY1=1 AND KEY2=1)

        일치하는 값을 발견하면 RC에 ZERO(0)값을 반환한다.*/

     rc=h.find();

 

     * NUM_ITEMS 속성은 HASH Object H에서 데이터 항목의 개수를 totalitems 데이터 세트 변수로 할당한다.;

     totalitems = h.num_items;

 

     /* DATA1과 위에서 생성한 TOTALITEMS 변수의 값을 LOG에 출력 */

     put data1=; 

     put totalitems=;

run;

 

* KEY : 'key1', 'key2', 'data1'; 

 

* KEY를 기준으로 중복을 제거한 유일한 관측치 값의 개수 카운트;

data dups;

     if _n_ = 1 then do;

        dcl hash h(dataset:'work.test');

        h.defineKey('key1', 'key2', 'data1'); * 키값 정의;

        h.defineDone();

        call missing(key1, key2, data1);

     end;

     totalitems = h.num_items;

     put totalitems=;

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:12 AM
Updated by:
Contributors

sas-innovate-white.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.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

Article Labels
Article Tags