/*測試數(shù)據(jù)集*/
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.的數(shù)值型格式穷躁。結(jié)果如下圖所示仅淑。
????????之后也會分享多種情況下的日期格式為yymmdd10.的字符型變量的拆解线脚。
感謝閱讀嗤军,歡迎關(guān)注:CDISC與SAS技肩。如有疑問撇吞,歡迎留言或者后臺私信交流,共同進步堆巧!