2011-10-31 3 views
5

데이터를 1000 회의 관측치로 설정했습니다. 마지막 관찰 만 인쇄하고 싶습니다. 다음을 사용 :SAS 데이터 세트의 마지막 관찰을 어떻게 인쇄합니까?

proc print data=apple(firstobs = 1000 obs = 1000); 
run; 

나는 마지막 관찰을 얻을 수 있습니다. 그러나 나는 나의 데이터 세트가 1000 번의 관찰을 가지고 있음을 미리 알아야한다. 이것을 알지 못하면 어떻게해야합니까?

+2

안녕 트레버, 질문은 "어떻게 관찰의 수를 얻을 수 있습니다 데이터 세트 "는 여기에 응답되었습니다 : http://stackoverflow.com/questions/ 5658994/how-to-detect-a-data-set-has-no-observations-in-sas –

+0

SAS Base 테스트를 위해 공부하고 여기에서 끝낸 사람을 위해 머리를 맞 춥니 다. 그들의 트릭 - LASTOBS 옵션과 같은 것은 없습니다! :) –

답변

7

당신이 할 수있는 많은 방법이 있습니다. 여기에 두 가지가 있습니다 :

proc sql noprint; 
select n(var1) into :nobs 
from apple; 
quit; 

proc print data=apple(firstobs=&nobs); run; 

이것은 매크로 변수에 대한 관측 수를 읽고 그 값을 사용하여 첫 번째 관측치를 지정합니다. (var1는 데이터의 변수를 참조합니다.)

또 다른 접근 방식을 마지막 관찰을 유지하는 데이터보기를 만들고 있음을 인쇄하는 것입니다 :

data tmp/view=tmp; 
set apple nobs=nobs; 
if _n_=nobs; 
run; 

proc print data=tmp; run; 
1

발견하는 방법에는 여러 가지가 있습니다 관측 수; 다음 매크로가 한 예입니다.

%macro nobs (dsn); 
    %let nobs=0; 
    %let dsid = %sysfunc(open(&dsn)); 
    %if &dsid %then %let nobs = %sysfunc(attrn(&dsid,nobs)); 
    %let rc = %sysfunc(close(&dsid)); 
    &nobs 
%mend nobs; 

%let n = %nobs(apple); 

proc print data=apple (firstobs=&n obs=&n); run; 
3

은 내가 SET, MERGE, MODIFY, 또는 UPDATE 문에 대한 end 옵션이 매우 유용하다고 생각합니다.

해결 방법 1 :

data x; 
    do i = 1 to 1000; 
    output; 
    end; 
run; 

data x; 
    set x end = _end; 
    end = _end; 
proc print data = x; 
    where end; 
run; 
0

는 두 가지 간단한 해결책이있다

data result; 
    set apple end=end; 
    if end then output; 
run; 
proc print data=result; 
run; 

해결 방법 2 :

data result; 
    set apple nobs=nobs; 
    if _N_=nobs then output; 
run; 
proc print data=result; 
run; 
관련 문제