BookmarkSubscribeRSS Feed

[SAS 프로그래밍 고수 백승민] 동일 관측치 내의 변수 값 중복 제거 및 변수 값 사전순으로 정렬하기

Started ‎06-11-2020 by
Modified ‎06-11-2020 by
Views 93

[질문] http://cafe.daum.net/statsas/B3m/13195

 [답변1]

 DATA BACK;
 input (var1-VAR7) ($);
datalines;
P271  P072  P070  P590  P070 P070 P111
P610  P60   P073  P220  P251 P610 P60
P340  Q550  T221  R94   P200 Q550 T221
 ;
run;

DATA BACK1;
 SET BACK;
     ARRAY test1(*) var1-var7;
     ARRAY test2(*) var1-var7;    
     DO i=1 to 7;
        DO j=I+1 to 7;
           * 동일한 값을 가지는 두번빼 값을 'ZZZZZ'으로 치환;
           * 아래 소트 과정중에서 높은 가중치 부여(공백은 소트시에 맨 앞으로 나옴);
           if i < j AND TEST1(i) = TEST2(j) THEN TEST2(j)='ZZZZZ';
           *put _all_;
        END;
     END;
     * 9.2 함수로 동일 관측치에 대하여 변수의 값 크기 순으로 정렬;
     CALL SORTC(of TEST1(*));
     * 최고 가중치 부여 값을 MISSING으로 변경;
     DO I=1 TO 7;
        IF TEST1(I) = 'ZZZZZ' THEN TEST1(I) = '';
     END;

     DROP I J;
RUN;

 

[9.1.3 버젼 사용시 변수값 정렬 참고]

* http://support.sas.com/kb/24/754.html

Sample 24754: Sort variable values within an observation

data one;
  input code1-code6;
datalines;
3 1 5 4 6 2
9 8 6 5 7 4
3 2 1 9 0 7
8 2 6 4 0 1
5 7 4 3 8 2
;

/* The DO UNTIL loop iterates until all of the variable values within an observation have          */
/* been sorted. Set SORTED to 1 and SORTED will be set to 0 each time the DO group executes        */
/* to reorder values. When that code does not execute, the array is already sorted, SORTED remains */
/* 1 and prevents the DO UNTIL loop from executing again.                                          */

data varsort(keep=code1-code6);
  array code(*) code1-code6;
  set one;
  do until (sorted);
    sorted=1;
    do i = 1 to dim(code)-1;
      if code(i) > code(i+1) then do;
        temp=code(i+1);
        code(i+1)=code(i);
        code(i)=temp;
        sorted=0;
      end;
    end;
  end;
run;

proc print data=varsort;
run;

 /* Alternative method for SAS 9.0 and above using the SMALLEST function. */

/* The SMALLEST function returns the kth smallest non-missing value.     */
/* To reorder from largest to smallest, use the LARGEST value instead.   */

data two;
  keep reordered:;
  set one;
  array code(6);
  array reordered(6);
  do k=1 to 6;
    reordered(k)=smallest(k, of code1-code6); 
  end;
run;

 

[답변2] JUNNAY님

 http://cafe.daum.net/statsas/B3m/13196

 저도 초보라 조금 지저분한 감이 있어 도움이 되실진 모르겠습니다.

나름대로 생각해서 프로그램을 짜보았습니다.

다른 더 좋은 방법이 많을 겁니다. ㅋ 찾아보면서 공부를 하면 좋을 것 같습니다. ^^

/*test data set*/ 

data t1;
 input  a1 $ a2 $ a3 $ a4 $ a5 $ @;
 datalines;
 P271  P072  P070  P590  P070
 P610  P60   P073  P220  P251
 P340 Q550  T221  R94    P200
 ;
 run;

/* 1.a1-a5를 하나의 변수로 생성=> x변수 생성     

    아래와 같이 그룹화        

기존변수       x        grp

a1              P271     0
a2              P072     0
a3              P070     0
a4              P590     0
a5              P070     0
a1              P610     1
a2              P60       1
a3              P073     1
a4              P220     1
a5              P251     1
a1              P340     2
a2              Q550     2
a3              T221     2
a4              R94       2
a5              P200     2    */ 

 data t2; set t1;
 array a[5] a1-a5;
 do i=1 to 5;
      x=a[i];
   output;
 end; 
    grp+1;
drop i a1-a5;
run;

 /*2.그룹별(grp)  중복값 제거 & 정렬 */
proc sort data=t2 out=t3  nodupkey;
by grp x ; run;

 /* 3. 그룹별 정렬 순으로 넘버링&

      array문을 통한 a1-a5 변수 생성

      => a1-a5변수의 값 할당 */

data t4; set t3;
/*grp_x: 그룹별 넘버링*/
 by grp;
 if first.grp then grp_x=1;
 else grp_x+1;
 array a[5] $ a1-a5 ;
 retain a1-a5 ;
 do i=1 to 5;
    if grp_x=i  then  a[i]=x;
end;
 if last.grp;
    tot_n=grp_x ;
 drop x grp  i grp_x ;
run;

Version history
Last update:
‎06-11-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