数学建模社区-数学中国

标题: 转载 分享 SAS基础宏之1:ChkFile 包括代码实现 [打印本页]

作者: 百年孤独    时间: 2013-7-23 15:24
标题: 转载 分享 SAS基础宏之1:ChkFile 包括代码实现
  在网上看到了一些比较不错的sas 关于宏的资源 希望对大家有用
; h: O) R9 Z4 l: w7 v 在此非常感谢 某某论坛的资源共享。
% r* h1 T, f  \; ]# Z1 o这个宏是用来检查指定目标文件夹或文件是否存在的,若存在则删除,不存在则创建,用于解决在dos中和SAS中无法创建文件夹的问题
- m4 c( ]3 r% c6 w2 [, C" o- r3 ]3 M例如可以在运行proc export前ChkFile下
+ K- _8 u' s8 y. S. w0 P" k! n' T" U1 \/ s
%macro ChkFile(OutFilePath);4 Q% y- V. r5 ^: ^$ A7 M
/**********************************************************************/, u7 `4 {. A/ }; T  \1 g+ N: k1 M
/* 此宏的作用是检查指定路径的文件夹或文件是否存在。若指定路径包含文件 */* N4 @" l* d6 m* w4 t+ b# j: e
/* 名,则运行此宏后路径中包含的文件夹存在但文件不存在,方便以后操作; */
% [2 p) J7 g4 b: S/* 若指定路径不包含文件名,则运行此宏后路径中包含的文件夹存在。其中, */
9 C, h" }% W; H/ p. Y; b9 W: u/* OutFilePath是指定的文件夹或文件路径。注意如果路径中包含文件名,则  */! x9 g1 J& M( h+ h
/* 一定要写全文件名和其扩展名。                                       */
; x; B. X. C+ w. G" i/*                                                                    */
: i8 K! z" N  U2 X* V/*                                      Created on 2011.9.29          */
/ V1 P! E7 S% P% @! x/*                                      Modified on 2011.9.29         */9 Y# b% L0 I8 n) G
/**********************************************************************/
3 H- c* t8 \1 s  L3 ~- m6 L1 L/ O0 J3 ^
/* 情形1:当输入参数OutFilePath包含文件名时,注意文件名必须带后缀 */$ U) f2 q3 y7 G8 k
%if %SYSFUNC(FIND(&OutFilePath,%Str(.))) NE 0 %then %do;
- h; w3 h) X" X: s        /* 得到OutFilePath中包含的文件名File和文件夹路径Dir */
% y3 F( R5 X- o. S7 l7 b  Z, `        %let File=%SYSFUNC(SCAN(&OutFilePath,-1,\));+ r) a$ B8 b6 Y/ I
        %let Dir=%SYSFUNC(SUBSTR(&OutFilePath,1,%EVAL(%SYSFUNC(LENGTH(&OutFilePath))-%SYSFUNC(LENGTH(&File)))));
2 ]7 D* {3 B3 Y% v4 g. Y. m% b% S
; D+ g- s5 E2 S/ z. I5 v# u        options noxwait;
& [5 @* V- ~2 C0 s8 f# d+ X
* i" S, T7 F  i4 f& w4 X0 ^2 T5 @        %local rc1 fileref1;% z3 C  a. ]% V+ v
        %local rc2 fileref2;
6 l8 p/ v0 k6 D3 i3 i        %let rc1=%SYSFUNC(FILENAME(fileref1,&Dir));, c! }6 z( S# h- y% b8 `
        %let rc2=%SYSFUNC(FILENAME(fileref2,&OutFilePath));
& N# X- i' g7 b) P3 t. U- @
/ F5 s, c' e+ e1 M: o        %if %SYSFUNC(FEXIST(&fileref1)) %then %do;
. @% D! C; o. E6 l  K) J8 ?8 q                %put NOTE: The directory "&Dir" exists.;
7 k: O/ Q& i" T4 c                %if %SYSFUNC(FEXIST(&fileref2)) %then %do;                /* 文件夹存在且文件也存在的情形 */! l0 Y0 }: B, @
                        %SYSEXEC del &OutFilePath;" r0 h/ J% p8 ^% z
                        %put NOTE: The file "&File" also exists, and has been deleted.;# g# i/ X- ~0 a) S& X
                        %end;
. P2 E1 [- O/ V) v- \                %else %put NOTE: But the file "&File" does not exist.;                /* 文件夹存在且文件不存在的情形 *// u& [! b8 _% D3 Z. p. w" @4 Q
                %end;
. _" r) i: M" u) o$ I        %else %do;                /* 文件夹不存在的情形 */
2 _& ]- H0 Q" M1 Y$ w                %SYSEXEC md &Dir;
, ^+ |' q; B8 C' d1 s9 X3 T8 @                %put %SYSFUNC(SYSMSG()) The directory has been created.;
) L/ c. B2 ~+ P* G9 G                %end;
4 a+ A' B  V% Z6 G9 X6 ^$ I+ F
* G. w. P# U: y0 a- B        %let rc1=%SYSFUNC(FILENAME(fileref1));
: n( a/ E& Z; N* y        %let rc2=%SYSFUNC(FILENAME(fileref2));; e2 p" a( ~- h" k( d' s* t. U
%end;
3 p% I* ^) q3 D* M8 Q2 }* g
0 ^6 e/ A" i- Z/ p/* 情形2:当输入参数OutFilePath不包含文件名时 */
* s9 R8 w" L4 U, u, S%else %do;
% k; c6 F/ p+ y6 v" G; ~2 O. {8 T        options noxwait;5 l* U& r5 M. G. p

/ J( f5 o) @9 O& N9 w8 Q6 q% ~7 a' W        %local rc fileref;( Y4 i0 B' F5 `/ M
        %let rc=%SYSFUNC(FILENAME(fileref,&OutFilePath));
: S! Z) k9 n  `, O( d& S! I( c        %if %SYSFUNC(FEXIST(&fileref))  %then
$ X8 Z& H: ^% d( H) i0 @                %put NOTE: The directory "&OutFilePath" exists.;
6 k0 b- w) v1 ~1 l* J  f  k7 I! {        %else %do; 7 s" M& g8 k7 c4 k4 W4 W
                %SYSEXEC md &OutFilePath;' h# {7 ?6 W, s& Q8 x) s% ]- c
                %put %SYSFUNC(SYSMSG()) The directory has been created.;
" b8 f" g5 G( d# _                %end; " L7 X% b9 }5 P5 e
        %let rc=%SYSFUNC(FILENAME(fileref));
* `# R" B: |7 U: r. J" z%end;0 i. [' y$ n+ L; s. i, p) n
8 V3 @8 p2 p0 V# [# i& ?3 K8 e6 }5 f
%mend;
4 x5 n8 G& y# a5 v
1 ~7 x( z6 B" g- n; e% @  l# s/ S) Q1 W3 W4 H5 N# H
%macro Demo();' Z- n  R; J' q0 }5 j& w# [

3 X7 @9 k2 X5 P& Q. P%ChkFile(d:\temp\data.xls);& L& u1 V8 j! t; n0 n7 r: N7 Y

; b# V& A# {& R$ d# b% M; q( j%mend;7 g5 Q# u% [) _3 V2 c' Z





欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5