BookmarkSubscribeRSS Feed
박은지
Calcite | Level 5


안녕하세요. SAS 매크로 질문 드립니다.

 

CD STRT_DT  TO_DT
C001 20181101 20191017
C001 20181101 20191017
C001 20181101 20191017
C001 20181101 20191017
C002 20190501 99991231
C002 20190501 99991231
C002 20190501 99991231
C003 20200305 99991231
C003 20200305 99991231
C003 20191018 99991231
C003 20191018 99991231
C004 20200305 20200511
C004 20200305 20200511
C004 20191101 20200511
C004 20191101 20200511
C005 20200305 99991231
C005 20200305 99991231
C005 20191216 99991231
C006 20200512 20200624
C006 20200512 20200624
C007 20200625 99991231
C007 20200625 99991231

이런식으로 데이터가 있는데

 

20160131부터 20201130까지 매월말 기준으로 발췌하는 매크로를 만들고 싶습니다.

20160131부터 20201130까지 매월말 기준으로 테이블을 발췌하는것까지는 했는데 해당내용을 매크로로 구하기는 어렵더라구요.

data a:

n=put (today(), yymmddn8. );

do until (n='20160131');

n=put(intnx('month, input(n,yymmddn8. ),-1,'e'), yymmddn8. );

output;

end;

run;

 

최종 적으로 원하는 테이블 형태는

기준년월 CD 운영시작일 운영종료일

20160131 ...

20160229 ...

20160331 ...

이렇게 만들고 싶습니다. 도와주세요ㅠㅠ

 

발췌조건) STRT_DT<=매월말<=TO_DT인 데이터 중에서
 중복된 CD별로 첫번째 있는 행만 발췌

 

proc sql;
create table table2 as
select *
from table1
where STRT_DT<='20160131'
and TO_DT>='20160131'
;

1 REPLY 1
Chulgyu1
SAS Employee

질문 내용을 100% 이해하지 못해서... 다음과 같이 해봤습니다.

매크로가 꼭 필요한 것 같지는 않아서 사용하지 않았습니다.

그리고, 20160131부터 시작하는 이유도 잘 모르겠네요. 조건을 적용하면 남는 데이터가 없거든요.

혹 잘못된 부분이나 추가 문의 있으시면 알려주세요.

data work.tmp1;
	infile datalines;
	input CD:$4. STRT_DT:yymmdd8.  TO_DT:yymmdd8.;
	format STRT_DT TO_DT yymmddn8.;
datalines;
C001 20181101 20191017
C001 20181101 20191017
C001 20181101 20191017
C001 20181101 20191017
C002 20190501 99991231
C002 20190501 99991231
C002 20190501 99991231
C003 20200305 99991231
C003 20200305 99991231
C003 20191018 99991231
C003 20191018 99991231
C004 20200305 20200511
C004 20200305 20200511
C004 20191101 20200511
C004 20191101 20200511
C005 20200305 99991231
C005 20200305 99991231
C005 20191216 99991231
C006 20200512 20200624
C006 20200512 20200624
C007 20200625 99991231
C007 20200625 99991231
;
run;

data work.end_dt;
	mth_end_dt=today();
	do until (mth_end_dt='31jan2016'd);
		mth_end_dt=intnx('month', mth_end_dt, -1, 'e');
		output;
	end;
	format mth_end_dt yymmddn8.;
run;

proc sql;
	create table work.tmp2 as
		select ee.*, tt.*
			from work.tmp1 tt, work.end_dt ee
			where tt.strt_dt<=ee.mth_end_dt and tt.to_dt>=ee.mth_end_dt
			order by 1,2
;
quit;

data work.final;
	set work.tmp2;
	by mth_end_dt cd;
	if first.cd;
run;