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
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
* 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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.