SAS:多个LOG的批量查询

OPTIONS PS=MAX LS=MAX NOCENTER SASMSTORE=SASUSER MSTORED MAUTOSOURCE;

/*获取指定文件夹的指定类型的所有文件*/

%MACRO GET_NAME(DIRNAME,TYP,OUT_TABLE)/STORE;/*参数有两个:路径,文件类型后缀*/   

%PUT %STR(----------->DIRNAME=&DIRNAME)        ;   

%PUT %STR(----------->TYP=&TYP)                ;   

DATA TEMP;       

RC=FILENAME("DIR","&DIRNAME");/*把&DIRNAME值传给文件引用符“DIR"*/      

OPENFILE=DOPEN("DIR");/*得到路径标示符OPENFILE,DOPEN是打开directory的sas内置函数*/   

IF OPENFILE>0 THEN DO;/*如果OPENFILE>0表示正确打开路径*/            

NUMMEM=DNUM(OPENFILE);/*得到路径标示符OPENFILE中member的个数nummem*/            

DO II=1 TO NUMMEM;                  

NAME=DREAD(OPENFILE,II);/*用DREAD依次读取每个文件的名字到NAME*/                 

filepath="&DIRNAME"||NAME;        

fnum=compress(NAME,".xml","");        

fnum=compress(fnum,"user_case","");                

OUTPUT;/*依次输出*/     

END;   

END;   

KEEP NAME filepath fnum;/*只保留filepath、fnum列*/

RUN;

PROC SORT DATA=TEMP;/*按照NAME排序*/    

BY DESCENDING NAME;    

%IF &TYP^=ALL %THEN %DO;/*是否过滤特定的文件类型&TYP*/        

WHERE  INDEX(UPCASE(NAME),UPCASE(".&TYP"));/*Y,则通过检索NAME是否包含&TYP的方式过滤文件类型*/    

%END; RUN;

DATA &OUT_TABLE(KEEP=NAME ID);  

SET TEMP;  

ID=_N_;

RUN;

PROC PRINT DATA=&OUT_TABLE;

RUN;

%MEND;  

/*查询LOG中的error与warning*/

%MACRO CHECK_LOG0(P,LOG_PATH)/STORE;

%IF &P=1 %THEN;

%ELSE %DO;  

DM 'log;"clear";';  RUN; QUIT;

%END;

%PUT CHECK AT &SYSDATE;

%PUT CHECK AT &SYSTIME;

%PUT #####################;

DATA _NULL_;  

INFILE "&LOG_PATH" END=LAST;

 RETAIN LINE 0;

 INPUT Key$ @@;

 line+1;  

IF line=1 THEN PUT"================================";  

IF Key in("ERROR:","ERROR","WARNING:") THEN DO;   

INPUT desc & $100.;   

PUT Key=desc;   

OUTPUT;  END;  

ELSE IF Key ="CHECK" THEN DO;   

INPUT desc & $10.;  

 PUT "Above was check" desc "----------------------";   

PUT;  END;  

ELSE INPUT Key $; RUN;

%MEND;

%MACRO CHECK_LOG(INPUT_TABLE,LOG_PATH)/STORE;

DATA _NULL_;  

SET &INPUT_TABLE NOBS=NOBS;  

CALL SYMPUTX('NUM',NOBS);  

STOP; RUN;

%DO I=1 %TO #  

DATA _NULL_;  SET &INPUT_TABLE;  

IF ID=&I;  

CALL SYMPUTX('LOG_NAME',NAME);  

STOP;

RUN; 

%PUT #####################; 

%PUT CHECK &LOG_NAME;  

%CHECK_LOG0(1,&LOG_PATH.\&LOG_NAME.);

%END;

%MEND;

/*调用宏*/

OPTIONS PS=MAX LS=MAX NOCENTER SASMSTORE=SASUSER MSTORED MAUTOSOURCE;

LIBNAME S '.\DATA';

%GET_NAME(.\DATA,log,S.DIR); /*参数1:log文件所在位置 参数2:读取文件类型 参数3:log文件名存储的数据集名称*/

%CHECK_LOG(S.DIR,.\DATA); /*参数1:log文件名存储的数据集名称 参数2:log文件所在位置*/

posted @ 2017-05-13 14:56  Anni爱摩天轮  阅读(736)  评论(0编辑  收藏  举报