/*测试数据集*/
data mydataset;
length mydate $20;
mydate='03MAR2023';output;
mydate='03MAR2023';output;
mydate='13MAR2023';output;
mydate='20MAR2023';output;
mydate='MAR2023';output;
mydate='2023';output;
mydate='UNMAR2023';output;
mydate='UNunk2023';output;
mydate='UNunkunun';output;
mydate='';output;
mydate='3 MAR 2023';output;
mydate='03 MAR 2023';output;
mydate='13 MAR 2023';output;
mydate='20 MAR 2023';output;
mydate='MAR 2023';output;
mydate='UN MAR 2023';output;
mydate='UN unk 2023';output;
mydate='UN unk unun';output;
run;
proc format;
invalue monfmt
'JAN'=1 'FEB'=2 'MAR'=3 'APR'=4 'MAY'=5 'JUN'=6 'JUL'=7 'AUG'=8 'SEP'=9 'OCT'=10 'NOV'=11 'DEC'=12;
run;
%macro sort(dataset,cutoffvar);
data &dataset.;
set &dataset.;
length varchar $20;
if prxmatch('/((\d{1,2})?(\s)?([A-Za-z]{3})?(\s)?(\d{4}))?|(\d{4})?(.*)?/',vvalue(&cutoffvar)) >0 then do;
date=prxchange('s/((\d{1,2})?(\s)?([A-Za-z]{3})?(\s)?(\d{4}))?|(\d{4})?(.*)?/$2$4$6$7/',-1,vvalue(&cutoffvar));
if prxmatch('/\d{4}/',vvalue(&cutoffvar)) then yr=input(prxchange('s/.*(\d{4})/$1/',-1,vvalue(&cutoffvar)),??best.);
if prxmatch('/[A-Za-z]{3}/',vvalue(&cutoffvar)) then mon=input(prxchange('s/.*([A-Za-z]{3}).*/$1/',-1,vvalue(&cutoffvar)),??monfmt.);
if prxmatch('/(^0(\d))|(^[1-9][0-9]?)(\s)?[A-Za-a]{3}/',vvalue(&cutoffvar)) then day=input(prxchange('s/(^0(\d))?|(^[1-9][0-9]?)?.*/$2$3/',-1,vvalue(&cutoffvar)),??best.);
varchar=catx('-',ifc(^missing(yr) ,put(yr,??best.)," "),ifc(^missing(mon) ,put(mon,??z2.)," "),ifc(^missing(day) ,put(day,??z2.)," "));
varnum=input(compress(varchar),??yymmdd10.);
end;
format varnum yymmdd10.;
keep &cutoffvar varchar yr mon day varnum date;
run;
%mend;
%sort(mydataset,mydate);
此次代码展示了多种情况下的日期格式为date9.的字符型变量的拆解,年月日方便进行计算或者比较大小,同时也输出了dtc/stdtc/endtc(ISO 8601)格式和完整yymmdd10.的数值型格式。结果如下图所示。
之后也会分享多种情况下的日期格式为yymmdd10.的字符型变量的拆解。
感谢阅读,欢迎关注:CDISC与SAS。如有疑问,欢迎留言或者后台私信交流,共同进步!