SAS统计软件如何读取凌乱数据,字符串转日期,并且再在字符串中截取字段?
发布网友
发布时间:2022-05-06 00:49
我来回答
共1个回答
热心网友
时间:2023-10-10 08:32
*将数据存放在名字为_pro的txt文档中,将整个一行数据读为一列,后用函数对该列数据进行拆分;
data clean._pro;
infile "f:\clean\_pro.txt" truncover;
format ori_string $200.;
input ori_string 1-200;
run;
data clean._pro2;
set clean._pro;
ori_string=compress(ori_string,"*/-& ") ;
format name $10. date_time $14. sex $2. job $20. hobby $20. x6 $30. age $10.;
n1=find(ori_string,"x1");
n2=find(ori_string,"x2");
n3=find(ori_string,"x3");
n4=find(ori_string,"x4");
n5=find(ori_string,"x5");
n6=find(ori_string,"x6");
n7=find(ori_string,"x7");
name=compress(compress(substr(ori_string,n1+2,n2-1),"x123456789:,/ "),",");
date_time=substr(compress(substr(ori_string,n2+2,n3-n2-1),"x:*,&/ "),3,14);
sex=substr(compress(substr(ori_string,n3+2,n4-n3-1),"x:*,&/ "),3,2);
job=substr(compress(substr(ori_string,n4+2,n5-n4-1),"x:*,&/ "),3,find(compress(substr(ori_string,n4+2,n5-n4-1),"x:*,&/ "),",")-2);
hobby=substr(compress(substr(ori_string,n5+2,n6-n5-1),"x:*,&/ "),3);
age=substr(compress(substr(ori_string,n7+2),""),3,find(compress(substr(ori_string,n7+2),""),"岁")-2);
x6=translate(compress(substr(ori_string,n6+2,n7-n6-1),"x*&/ "),",",",");
if _n_=1 then prx=prxparse("/:\d+,/");
retain prx;
start=1;
stop=length(x6);
call prxnext(prx,start,stop,x6,position,length);
array x[4]$;
do i=1 to 4 while (position gt 0);
x[i]=substr(x6,position+1,length-2);
call prxnext(prx,start,stop,x6,position,length);
end;
keep name date_time sex job hobby x1-x4 age;
run;