SAS datechange 去UK NK 宏(原创)
random 每次赋值需要新变量,不然会错
%macro datechange(indata=,middata=,outdata=,var1=,midvar2=,random=);
data &middata.;
set &indata.;
length &midvar2. $200.;
if indexc(upcase(&var1.),"年月日") then do; a=tranwrd(&var1.,"年","-");b=tranwrd(a,"月","-");c=tranwrd(b,"日"," ");&var1=c;end;
if index(upcase(&var1.),"-NA") then &var1.=tranwrd(&var1.,"-NA"," ");
if index(upcase(&var1.),"UK") then &midvar2.=strip(tranwrd(upcase(&var1.),"-UK"," "));
else if index(upcase(&var1.),"JUN") then &midvar2.=strip(tranwrd(upcase(&var1.),"JUN","JUN"));
else if index(upcase(&var1.),"UN") then &midvar2.=strip(tranwrd(upcase(&var1.),"-UN"," "));
else &midvar2.=&var1.;
if 4<length(strip(&midvar2.))<=8 then &random.=put(input(&midvar2.,anydtdte.),yymmdd10.);
if length(strip(&midvar2.))=11 then &var1.=put(input(&midvar2.,date11.),yymmdd10.);
if length(strip(&midvar2.))=4 then &var1.=&midvar2.;
run;
data &outdata.;
set &middata.;
if &var1.=" " or not missing(&random.) then &var1.=substr(&random.,1,7);
&var1.= compress(&var1.,,"a");
if indexc(&var1.,"0123456789")=0 then call missing(&var1.);
run;
%mend datechange;
/*%datechange(indata=raw.mh1,middata=mh1_1,outdata=mh1_2,var1=mhdat,midvar2=riqi,random=fff);*/