Hi.
I have a duty to send out data everyday to a reciever.
I want to make this code good for automatisation
Is there a macro where It reads the file name with different dates every day, without me changing the filename?
the filename could be something like this today: test_data_2021-01-19, and tomorrow test_data_2021-01-20
I also have file without the date, so a macro that also can search for the latest file in a directory?
Hope it is understandable
See this quick data step to read all files from a directory and get their last modification timestamp:
%let dir=/folders/myfolders;
data dir;
length
fref dref $8
fname $200
;
format dt e8601dt19.;
rc = filename(dref,"&dir.");
did = dopen(dref);
do i = 1 to dnum(did);
fname = catx("/","&dir.",dread(did,i));
rc = filename(fref,fname);
fid = fopen(fref);
if fid ne 0
then do;
dt = input(finfo(fid,foptname(fid,5)),nldatm40.);
output;
rc = fclose(fid);
end;
else do;
err = sysmsg();
put _all_;
end;
end;
rc = dclose(did);
keep fname dt;
run;
You can then either sort by name or dt to get the latest file, or select for a given name with a given date in it.
You are not really clear about this.
Do you receive files for importing into SAS, or do you need to export data to external files for sending them to someone else?
i receive files from someone into a directory, everyday - and everyday I use these files into my SAS program to come out with a result for them.
See this quick data step to read all files from a directory and get their last modification timestamp:
%let dir=/folders/myfolders;
data dir;
length
fref dref $8
fname $200
;
format dt e8601dt19.;
rc = filename(dref,"&dir.");
did = dopen(dref);
do i = 1 to dnum(did);
fname = catx("/","&dir.",dread(did,i));
rc = filename(fref,fname);
fid = fopen(fref);
if fid ne 0
then do;
dt = input(finfo(fid,foptname(fid,5)),nldatm40.);
output;
rc = fclose(fid);
end;
else do;
err = sysmsg();
put _all_;
end;
end;
rc = dclose(did);
keep fname dt;
run;
You can then either sort by name or dt to get the latest file, or select for a given name with a given date in it.
For a file with different dates - where in the code could I state that
You use a simple IF condition with SUBSTR() or SCAN() of the filename.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.