data a;
input stock $8. @13 month 8. @24 index 8.4;
datalines;
000001 192801 0.3
000001 192802 0.4
000001 192803 0.3
000001 192804 0.4
000001 192805 0.3
000001 192806 0.4
000001 192807 0.3
000001 192809 0.4
000002 192801 0.3
000002 192802 0.8
000002 192804 0.4
000002 192805 0.3
000002 192806 0.4
000002 192807 0.3
000002 192808 0.4
;
run;
proc sort data=a;
by stock month;
run;
data b;
set a;
retain bg_mon 0;
by stock month;
if first.stock then do;bg_mon=month;end;
if last.stock then do;
year1=input(substr(compress(put(bg_mon,8.)),1,4),8.);
month1=input(substr(compress(put(bg_mon,8.)),5,2),8.);
year2=input(substr(compress(put(month,8.)),1,4),8.);
month2=input(substr(compress(put(month,8.)),5,2),8.);
if year1=year2 then do month3=month1 to month2;
year3=year1;
if month3<10 then do;
month4=input(compress(put(year3,8.))||'0'||compress(put(month3,8.)),8.);
end;
else do;
month4=input(compress(put(year3,8.))||compress(put(month3,8.)),8.);
end;
output;
end;
else do year3=year1 to year2;
if year3=year1 then do month3=month1 to 12;
if month3<10 then do;
month4=input(compress(put(year3,8.))||'0'||compress(put(month3,8.)),8.);
end;
else do;
month4=input(compress(put(year3,8.))||compress(put(month3,8.)),8.);
end;
output;
end;
else if year3=year2 then do month3=1 to month2;
if month3<10 then do;
month4=input(compress(put(year3,8.))||'0'||compress(put(month3,8.)),8.);
end;
else do;
month4=input(compress(put(year3,8.))||compress(put(month3,8.)),8.);
end;
output;
end;
else do month3=1 to 12;
if month3<10 then do;
month4=input(compress(put(year3,8.))||'0'||compress(put(month3,8.)),8.);
end;
else do;
month4=input(compress(put(year3,8.))||compress(put(month3,8.)),8.);
end;
output;
end;
end;
end;
keep stock month4;
rename month4=month;
run;
proc sort data=b;
by stock month;
run;
data a;
merge a(in=a) b(in=b);
by stock month;
if b;
run;
数据集a中,index缺失的便是缺失的月份