用來檢查數(shù)據格式的
好像SAS沒有判斷日期變量的好辦法,只能看格式是不是DATE9.,YYMMDD之類的了
如果有其他辦法,記得通知我
- %macro ChkVarFormat(SourceTable,InputVar,FlagVarFormat);
- /**********************************************************************/
- /* 此宏的作用是檢查某數(shù)據表中指定變量的格式,如日期格式YYMMDD10.和字 */
- /* 符串格式$40.。其中,SourceTable是原始表格;InputVar是指定的變量, */
- /* 若為多個變量,請用空格分隔;FlagVarFormat是標記宏變量名稱,若有多 */
- /* 個InputVar變量,則用小數(shù)點分隔。 */
- /* */
- /* 最終得到的是宏變量&FlagVarFormat。 */
- /* */
- /* Created on 2012.9.7 */
- /* Modified on 2012.9.18 */
- /**********************************************************************/
- %if %SYSFUNC(FIND(&SourceTable,.)) NE 0 %then %do;
- %let CVF_LibName=%UPCASE(%SCAN(&SourceTable,1,.));
- %let CVF_MemName=%UPCASE(%SCAN(&SourceTable,2,.));
- %end;
- %else %do;
- %let CVF_LibName=WORK;
- %let CVF_MemName=%UPCASE(&SourceTable);
- %end;
- %ChkDataSet(DataSet=&CVF_LibName..&CVF_MemName,FlagDataSetExists=CVF_FlagDataSetExists);
- %if &CVF_FlagDataSetExists EQ 1 %then %do;
- %ChkVar(SourceTable=&SourceTable,InputVar=&InputVar,FlagVarExists=CVF_FlagVarExists);
- %if %SYSFUNC(FIND(&CVF_FlagVarExists,0)) EQ 0 %then %do;
- proc contents data=&SourceTable out=CVF_temp(keep=NAME TYPE FORMAT FORMATL) noprint;
- run;
- %global &FlagVarFormat;
- %if %SYSFUNC(FIND(&InputVar,%STR( ))) NE 0 %then %do;
- %SeparateString(InputString=&InputVar,OutputString=CVF);
- %do CVF_i=1 %to &CVF_Num;
- %let CVF_FlagVarExists&CVF_i=0;
- data _null_;
- set CVF_temp;
- if UPCASE(NAME)=UPCASE("&&CVF_Var&CVF_i.") then do;
- call symputx("CVF_FlagVarFormat&CVF_i",FORMAT);
- call symputx("CVF_FlagVarFormatL&CVF_i",FORMATL);
- end;
- run;
- %end;
- %let &FlagVarFormat=&&CVF_FlagVarFormat1.&&CVF_FlagVarFormatL1;
- %do CVF_j=2 %to &CVF_Num;
- %let &FlagVarFormat=&&&FlagVarFormat.%STR(.)&&CVF_FlagVarFormat&CVF_j&&CVF_FlagVarFormatL&CVF_j;
- %end;
- %end;
- %else %do;
- data _null_;
- set CVF_temp;
- if UPCASE(NAME)=UPCASE("&InputVar.") then do;
- call symputx("CVF_FlagVarFormat",FORMAT);
- call symputx("CVF_FlagVarFormatL",FORMATL);
- end;
- run;
- %let &FlagVarFormat=&CVF_FlagVarFormat.&CVF_FlagVarFormatL;
- %end;
- %end;
- %else %do;
- %put ERROR: There is no variable named %SCAN(&InputVar,%SYSFUNC(FIND(&CVF_FlagVarExists,0))), please check it again.;
- %goto exit;
- %end;
- %end;
- %else %if &CVF_FlagDataSetExists EQ 0 %then %do;
- %put ERROR: The DataSet "&SourceTable." does not exist, please check it again.;
- %goto exit;
- %end;
- %else %do;
- %put ERROR: There may be something wrong with the macro ChkDataSet, please contact the writer for help.;
- %goto exit;
- %end;
- /* 若要顯示&FlagVarFormat的值,請取消下面的注釋 */
- /*%put &FlagVarFormat=&&&FlagVarFormat;*/
- /* 刪除不必要的表格 */
- proc delete data=CVF_temp;
- run;
- %exit:
- %mend;
- %macro Demo();
- %let SourceTable=AOQR_DetHoldingsOfFund_Chosen;
- %let InputVar=End_Date Fund_Name Fund_Code;
- %let FlagVarFormat=FlagVarFormat1;
- %ChkVarFormat(&SourceTable,&InputVar,&FlagVarFormat);
- %mend;