SAS 处理不规则时间变量的方法
形如下图,假设该表为PR2数据集内变量,则有如下代码
data pr3;
set pr2;
length date $200.;
if index(upcase(PRSTDAT1),"UK") then date=strip(tranwrd(upcase(PRSTDAT1),"UK-"," "));
else if index(upcase(PRSTDAT1),"JUN") then date=strip(tranwrd(upcase(PRSTDAT1),"JUN-","06"));
else if index(upcase(PRSTDAT1),"UN") then date=strip(tranwrd(upcase(PRSTDAT1),"UN-"," "));
else date=PRSTDAT1;
if 4<length(strip(date))<=8 then rrr=put(input(date,anydtdte.),yymmdd10.); anydtdte:只能精确到月份
if length(strip(date))=11 then PRSTDAT2=put(input(date,date11.),yymmdd10.);
if length(strip(date))=4 then PRSTDAT2=date;
run;
data pr3_1;
set pr3;
if PRSTDAT2=" " or not missing (rrr) then PRSTDAT2=substr(rrr,1,7);
PRSTDAT1=PRSTDAT2;
run;
最后结果如下:
set pr2;
length date $200.;
if index(upcase(PRSTDAT1),"UK") then date=strip(tranwrd(upcase(PRSTDAT1),"UK-"," "));
else if index(upcase(PRSTDAT1),"JUN") then date=strip(tranwrd(upcase(PRSTDAT1),"JUN-","06"));
else if index(upcase(PRSTDAT1),"UN") then date=strip(tranwrd(upcase(PRSTDAT1),"UN-"," "));
else date=PRSTDAT1;
if 4<length(strip(date))<=8 then rrr=put(input(date,anydtdte.),yymmdd10.); anydtdte:只能精确到月份
if length(strip(date))=11 then PRSTDAT2=put(input(date,date11.),yymmdd10.);
if length(strip(date))=4 then PRSTDAT2=date;
run;
data pr3_1;
set pr3;
if PRSTDAT2=" " or not missing (rrr) then PRSTDAT2=substr(rrr,1,7);
PRSTDAT1=PRSTDAT2;
run;
最后结果如下: