SASUnit Examples  Version 1.2.1
_sasunit_dir.sas
Go to the documentation of this file.
1 
28 /* version history
29  29.01.2013 KL Removed support for filenames with unbalanced brackets.
30  17.10.2012 KL extended for english version of windows. In English Windows7 x64 there is an additional AM/PM column in the directory listing.
31  Therefore an new datastep variable is included to reflect this circumstances. SCAN CANNOT be used because there may be blanks
32  in the filenames and blank is the delimiter between the columns. So we need to stick to specific character positions
33  14.07.2009 AM extended for english version of windows
34  02.10.2008 NA modified for LINUX
35  10.02.2008 AM Dokumentation verbessert
36  15.12.2007 AM Abfrage nach sysrc entfernt, weil sysrc>0, wenn keine Datei gefunden;
37  Dateinamen mit slashes statt backslashes
38 */
39 
40 %MACRO _sasunit_dir(
41  i_path=
42  ,i_recursive=0
43  ,o_out=dir
44 );
45 
46 %if &sysscp. = WIN %then %do;
47  %local dirindicator_en dirindicator_de encoding;
48  %let dirindicator_en=Directory of;
49  %let dirindicator_de=Verzeichnis von;
50  %let encoding=pcoem850;
51  %let i_path = %sysfunc(translate(&i_path,\,/));
52 
53  proc sql noprint;
54  create table &o_out (filename char(255));
55  quit;
56  %IF &syserr NE 0 %THEN %GOTO errexit;
57 
58  %local xwait xsync xmin;
59  %let xwait=%sysfunc(getoption(xwait));
60  %let xsync=%sysfunc(getoption(xsync));
61  %let xmin =%sysfunc(getoption(xmin));
62 
63  options noxwait xsync xmin;
64 
65  %local dirfile;
66  %let dirfile=%sysfunc(pathname(work))\___dir.txt;
67  filename _dirfile "&dirfile" encoding=&encoding;
68  %local s;
69  %IF &i_recursive %then %let s=/S;
70  %put SYSEXEC(dir &s "&i_path" > "&dirfile");
71  %SYSEXEC(dir &s /a-d "&i_path" > "&dirfile");
72 
73  options &xwait &xsync &xmin;
74 
75  data &o_out (keep=filename changed);
76  length dir filename $255 language $2;
77  retain language "__" dir FilePos;
78  infile _dirfile truncover;
79  input line $char255. @;
80  if index (line, "&dirindicator_en") or index (line, "&dirindicator_de") then do;
81  if index (line, "&dirindicator_en") then do;
82  dir = substr(line, index (line, "&dirindicator_en")+length("&dirindicator_en")+1);
83  end;
84  else do;
85  dir = substr(line, index (line, "&dirindicator_de")+length("&dirindicator_de")+1);
86  end;
87  end;
88  if substr(line,1,1) ne ' ' then do;
89  * Check for presence of AM/PM in time value, because you can specify AM/PM timeformat in German Windows *;
90  if (language = "__") then do;
91  Detect_AM_PM = upcase (scan (line, 3, " "));
92  if (Detect_AM_PM in ("AM", "PM")) then do;
93  Filenamepart = scan (line,5, " ");
94  Filepos = index (line, trim(Filenamepart));
95  language = "EN";
96  end;
97  else do;
98  Filenamepart = scan (line,4, " ");
99  Filepos = index (line, trim(Filenamepart));
100  language = "DE";
101  end;
102  end;
103  if language='DE' then do;
104  input @1
105  d ddmmyy10. +2
106  t time5.
107  ;
108  end;
109  else do;
110  input @1
111  d mmddyy10. +2
112  t time9.
113  ;
114  end;
115  changed = dhms (d, hour(t), minute(t), 0);
116  format changed datetime20.;
117  filename = translate(trim(dir) !! '/' !! substr (line,FilePos),'/','\');
118  output;
119  end;
120  run;
121 
122  filename _dirfile;
123 %end; /* &sysscp. = WIN */
124 
125 %else %if &sysscp. = LINUX %then %do;
126 
127  %LOCAL dirfile encoding s;
128 
129  data &o_out ;
130  length filename $255;
131  format changed datetime20.;
132  run;
133 
134  %IF &syserr NE 0 %THEN %GOTO errexit;
135 
136  %LET encoding=wlatin1;
137  %LET dirfile=%sysfunc(pathname(work))/.dir.txt;
138  filename _dirfile "&dirfile" encoding=&encoding;
139 
140  %put Directory search is: &i_path;
141  %IF &i_recursive=0 %then %let s=-maxdepth 1;
142  %SYSEXEC(find -P &i_path. &s. -type f -printf "%nrstr(%h/%f\t%TD\t%TT\t\r\n)" > &dirfile. 2>/dev/null);
143 
144  data &o_out (keep=filename changed);
145  length filename $255;
146  format changed datetime20.;
147  infile _dirfile delimiter='09'x truncover;
148  input filename $ d:mmddyy8. t:time8.;
149  changed = dhms (d, hour(t), minute(t), 0);
150  run;
151 
152 %end; /* &sysscp. = LINUX */
153 
154 %errexit:
155 %MEND _sasunit_dir;