앞에서부터 N 건만 읽을 때는 OBS= 를 쓴다. 구간을 지정하려면 FIRSTOBS= 를 함께 준다.
data want;
set have(obs=1000000);
run;
data want;
set have(firstobs=500001 obs=1000000);
run;
proc sql;
create table want as
select * from have(obs=1000000);
quit;
OBS= 는 순차적으로 읽으면서 제한하므로 대용량에서는 시간이 걸린다. 표본만 필요하면 먼저 작은 값으로 확인한다.
CAS 테이블에서는 액션이나 FedSQL 을 쓴다.
proc cas;
table.fetch / table={name="have", caslib="casuser"}, to=100;
quit;
proc fedsql sessref=mysess;
create table casuser.want as
select * from casuser.have limit 1000000;
quit;
테이블을 지우는 것은 PROC DATASETS 의 DELETE 다.
proc datasets library=work nolist;
delete mytable;
quit;
proc datasets library=mylib nolist;
delete table1 table2 table3;
quit;
이름 규칙으로 지울 때는 콜론 접미사를 쓴다. temp_: 는 temp_ 로 시작하는 모든 테이블이다.
proc datasets library=work nolist;
delete temp_:;
quit;
라이브러리 전체를 비우는 KILL 은 work 외의 영구 라이브러리에서는 위험하다.
proc datasets library=work kill nolist;
quit;
존재 여부를 확인하고 지우면 로그가 깔끔하다.
%macro del_if_exist(lib, ds);
%if %sysfunc(exist(&lib..&ds)) %then %do;
proc datasets library=&lib nolist;
delete &ds;
quit;
%end;
%mend;
%del_if_exist(work, mytable);
DROP 은 테이블이 아니라 변수를 지운다. drop mytable; 같은 구문은 SAS 에 없다.
data work.new;
set work.old(drop=col1 col2);
run;
proc sql;
alter table mylib.mytable drop col1, col2;
quit;
| 목적 | 방법 |
|---|---|
| 컬럼 삭제 | DROP |
| 테이블 삭제 | PROC DATASETS ... DELETE |
| 라이브러리 전체 삭제 | PROC DATASETS ... KILL |
| CAS 테이블 삭제 | PROC CASUTIL ... DROPTABLE |
LIBNAME 으로 디렉터리를 라이브러리로 연결한 뒤 그 라이브러리에 데이터셋을 만든다.
libname mylib "/sasdata/shared";
data mylib.sales_2025;
input sales_dt : yymmdd10. branch $10. amount;
format sales_dt yymmdd10.;
datalines;
2025-01-01 SEOUL 1000
2025-01-02 BUSAN 2000
;
run;
/sasdata/shared/sales_2025.sas7bdat
디렉터리 존재 여부를 먼저 확인한다.
%put %sysfunc(fileexist(/sasdata/shared));
Libref ... is not assigned 또는 Write access to member denied 가 나오면 OS 권한, 읽기 전용 마운트, 컨테이너 환경이면 파드에 마운트됐는지를 본다.
SYSERR 또는 SYSCC 매크로 변수로 직전 스텝의 결과를 확인한다.
%macro run_pipeline;
%a;
%if &syserr > 0 %then %do;
%put ERROR: A 매크로 실행 중 오류가 발생해 후속 작업을 중지합니다.;
%return;
%end;
%b;
%mend;
%run_pipeline;
SYSERR 은 직전 DATA step 또는 PROC 의 결과만 담고 다음 스텝에서 덮어쓰이므로, 검사 시점을 스텝 직후로 잡아야 한다. 파이프라인 전체의 누적 상태를 보려면 SYSCC 를 쓴다.
WARNING: Some character data was lost during transcoding.
WARNING: Character values have been truncated.
이 두 줄이 나오면 문자 인코딩 변환 문제다. 먼저 세션과 데이터셋의 인코딩을 본다.
proc options encoding;
run;
%put &=sysencoding;
proc contents data=mylib.mytable;
run;
SAS 의 문자 변수 길이는 바이트 기준이다. UTF-8 에서 한글 한 글자는 3바이트이므로 EUC-KR 기준으로 잡은 길이는 부족해진다.
proc sql;
select max(lengthc(col)) as char_len,
max(length(col)) as byte_len
from mylib.mytable;
quit;
data work.problem;
set mylib.mytable;
if length(col) > 100; /* 정의된 길이 기준 */
run;