Desktop productivity for business analysts and programmers

checking dates under dir

Reply
Super Contributor
Super Contributor
Posts: 318

checking dates under dir

Hello friends,

 

Goal is pull details info for files under multiple directories - running code in sas eg agianst linux environment,,,

 

i have

&x macro var resolves to dir1, dir2, dir3 and

&numoffiles resolves to 3

 

all directories (dir1, dir2 and dir3) contains few other files and i want to pull all details about those files (separately for dir1 dir2 dir3) 

/xyz/dir1

/xyz/dir2

/xyz/dir3

 

/*This data step not working fine   <-- what modification needs to be done  <- if you can please help*/

 

%macro test;

%do i=1 %to &numoffiles.;

data repo.file&i. (drop=month day);

length access $ 20 files $ 200;

infile "cd /xyz/&temp. ; ls -lt;" pipe firstobs=2;

input var1 $ var2 $ var3 $ var4 $ size $ month $ day $ time $ files;

date=input(catt(day,month,'2017'),date9.);

format date date9.;

run;

%end;

%mend;

%test;

 

 

so output would be like,

file1 dataset should have all those var (mentioned in input statement) with files details for dir1

file2 dataset should have all those var (mentioned in input statement) with files details for dir2

file3 dataset should have all those var (mentioned in input statement) with files details for dir3

 

Valued Guide
Posts: 505

Re: checking dates under dir

I don't have unix right here, however this may also work as is in unix.

I do have macro utilities to parse ls -l output into a SAS dataset. But it is somewhat advances.

%macro unx(cmd=dir,dir=d:/txt);
  x "cd &dir";
  filename xeq pipe "&cmd";
  data _null_;
    infile xeq;
    input;
    putlog _infile_;
  run;quit;
%mend unx;

%unx;

 Volume in drive D is data
 Volume Serial Number is 825D-0460

 Directory of d:\txt

03/01/2017  02:43 PM    <DIR>          .
03/01/2017  02:43 PM    <DIR>          ..
09/14/2016  02:12 PM                35 area.txt
03/01/2017  06:54 AM               247 class.txt
03/01/2017  03:36 PM             1,704 longtext.txt
09/11/2016  05:54 PM               406 pym.txt
09/12/2016  08:05 AM               384 sink.txt
12/12/2016  11:59 AM                28 symbols.txt
11/05/2016  07:24 PM                76 test.txt
08/16/2016  11:29 AM               167 test.txt.txt
11/21/2016  09:41 AM               227 txtchg.txt
11/21/2016  08:44 AM                76 txtnew.txt
11/21/2016  08:44 AM                77 txtold.txt
08/06/2016  05:05 PM               246 utl_pyjson.txt
02/05/2017  11:36 AM               169 varlist.txt
08/14/2016  06:19 AM    <DIR>          vdo
11/26/2016  05:21 AM                84 walrus.txt
12/22/2016  05:58 AM                 0 xml_list.txt
              18 File(s)          9,571 bytes
               3 Dir(s)  429,669,924,864 bytes free
Valued Guide
Posts: 505

Re: checking dates under dir

* there is an issue with dates when using unix "ls-l".
 current dates (past year?) do not have the year.
 I think the perl code below fixes that and gives much
  more accurate file data and times?

These utilities might help? /* T000411 FILENAMES IN UNIX OR WINDOWS DIRECTORY INTO A SAS DATASET %macro utldatadir(pth); data dir; rc=filename("mydir","&pth"); did=dopen("mydir"); if did > 0 then do; memcount=dnum(did); do i=1 to memcount; lstname=dread(did,i); t_cnt+1; output; end; end; run; proc print; var lstname t_cnt; run; %mend utldatadir; %utldatadir(c:\rtf); /* T000412 FILENAMES, USER AND SIZE IN A UNIX DIRECTORY INTO A SAS DATASET %macro dirlst(pth=%str(.)); filename oecmd pipe "ls -l &pth"; data dirlst; length fyl $100 usr $16 siz $20; infile oecmd truncover lrecl=500 pad; input; fyl=left(reverse(scan(left(reverse(_infile_)),1,' '))); usr=scan(_infile_,3,' '); siz=scan(_infile_,4,' '); dte=substr(_infile_,index(_infile_,strip(siz))+length(strip(siz))); dte=substr(dte,1,index(dte,fyl)-1); run; %mend dirlst; /* T000370 USING PERL TO GET UNIX DATETIME IN SECONDS FOR ALL OBJECTS IN A DIRECTORY IGNORE WARNING ABOUT %S */ %let pth=/home/regusers/local/utl/rtf; %let outlst=dates; data _null_; call system("cd &pth"); call system("setenv PWD &pth"); run; %let pwd=%sysget(PWD); %put pwd=&pwd; filename getdte pipe "perl -e 'foreach(@ARGV){$t =localtime ( ( ( stat ( $_ ) ) [9] ) ); printf(qq{%-64s%s\n},$_,$t);}' *"; data &outlst(index=(program/unique)); format cvtdte datetime23.1 sdfdtetym date9.; infile getdte; input program $64. @69 getmth $3. @73 getday $3. @85 getyer $4. @76 gettym $8.; sav=cats(getday,getmth,getyer,':',gettym); cvtdte=input(cats(getday,getmth,getyer,':',gettym),datetime18.); sdfdtetym=datepart(cvtdte); fulfyl=cats("&pth",'/',program); if index(program,'.')>0; if not (program =: 'init'); keep program sdfdtetym fulfyl cvtdte; run; filename getdte clear; proc print data=_last_;run;
Super Contributor
Super Contributor
Posts: 318

Re: checking dates under dir

where I was trying to get is applying "cd" command to each dir (dir1/dir2/dir3) and get output in 3 different sas datasets which shows dir or files details for those 3 dir...

so output would be for example,

datasets=file1
var= var1 $ var2 $ var3 $ var4 $ size $ month $ day $ time $ files;

datasets=file2
var= var1 $ var2 $ var3 $ var4 $ size $ month $ day $ time $ files;

datasets=file3
var= var1 $ var2 $ var3 $ var4 $ size $ month $ day $ time $ files;
Ask a Question
Discussion stats
  • 3 replies
  • 138 views
  • 0 likes
  • 2 in conversation