PROC IMPORT OUT=sheet1
DATAFILE= "C:\Users\wang\Desktop\data.xlsx"
DBMS=EXCEL REPLACE;
GETNAMES=YES;
SHEET="sheet1";
RUN;
proc sql;
create table s as select min(Begin_date) as min , max(End_date) as max from sheet1;
quit;
data s1;
set s;
ys=year(min);
ms=month(min);
ye=year(max);
me=month(max);
m=(ye-ys)*12+me-ms;
output;
do i=1 to m;
ms+1;
output;
end;
run;
data s2;
set s1 (keep= ys ms);
format stdate enddate date9.;
if ms>12 & mod(ms,12)^=0 then do;
ys=ys+int(ms/12);
ms=mod(ms,12);
end;
else if ms>12 & mod(ms,12)=0 then do;
ys=ys+int(ms/12)-1;
ms=12;
end;
stdate=mdy(ms,1,ys);
if ms<10 then m='0'||put(ms,best.);else m=put(ms,best.);
yymm=put(ys,best.)||m;
enddate=intnx('month',input(compress(yymm),yymmn6.),0,'end');
keep ys ms stdate enddate;
run;
data s3;
set sheet1;
do i=1 to all;
set s2 point=i nobs=all;
if Begin_date <=stdate<=End_date | Begin_date <=enddate<=End_date
then s=1 ;else s=0;
output;
end;
run;
data s4;
set s3(keep=id Begin_date End_date ys ms s);
if ms<10 then ym=catx('-','0'||ms,ys);else ym=catx('-',ms,ys);
ym=compress(ym);
drop ys ms;
run;
proc sort data=s4;
by id Begin_date End_date;
run;
proc transpose data=s4 out=final(drop=_name_) name=_name_ prefix=s;
by id Begin_date End_date;
id ym;
var s;
run;
PROC EXPORT DATA= final
OUTFILE= "d:\final.XLSX"
DBMS=excel REPLACE;
SHEET="sheet1";
RUN;