Hello,
There is a Folder that contain multiple txt daily files with name structure "Offers_YYYYMMDD"
(For example: Offers_20220701 is file for 01JULY2022 ).
The files are created almost everyday (non included non-business days and non included cases of fault running).
There is a data set called All_Days_tbl that contain all dates between 01JAN2021 and Today.
In this data set there is a column called "path_to_Check" that contain the File name that need to check if it exists or not.
The task is to create a new column called "Indicator_File_Exists" in data set "All_Days_tbl " .
The column will get value 1 if the file exists and 0 if not .
Please note that I don't want to read the files (I just need to know which file exists and which not).
What is the way to do it please?
%let start_sas_date="01JAN2021"d;
%let Last_sas_date= "%sysfunc(today(),date9.)"d;
%put &start_sas_date;
%put &Last_sas_date;
data All_Days_tbl_(DROP=end_date);
date=&start_sas_date.;
end_date=&Last_sas_date.;
format date end_date date9.;
do while (date<=end_date);
output;
date=intnx('day', date, 1, 's');
end;
format date date9.;
run;
Data All_Days_tbl;
SET All_Days_tbl_;
path_to_Check=CATS("/usr/local/SAS/offers_daily_Files/Loans.recomend.daily.",put(date,YYMMDDn8.));
Run;
No need for any macro variables and only your result data set is needed:
data example; length path_to_Check $ 200; do date="01JAN2021"d to today(); path_to_Check=CATS('"',"/usr/local/SAS/offers_daily_files/Loans.recomend.daily.",put(date,YYMMDDn8.),'"'); ind = fileexist(path_to_Check); output; end; run;
The fileexist returns 1 for found and 0 for not found so the IF /Then/ construct is a waste of clock cycles.
The iterated do will be easier to follow instead of the DO WHILE and avoids any possible infinite loop from a typo involving the incrementing of the dates.
For most purposes formatted date values just make macro coding difficult. The main usage would be in text that people read such as Title, Footnote text or file names.
Okay,
I found the solution.
Here is the solution.
If there are other solutions I will be glad to see.
%let start_sas_date="01JAN2021"d;
%let Last_sas_date= "%sysfunc(today(),date9.)"d;
%put &start_sas_date;
%put &Last_sas_date;
data All_Days_tbl_(DROP=end_date);
date=&start_sas_date.;
end_date=&Last_sas_date.;
format date end_date date9.;
do while (date<=end_date);
output;
date=intnx('day', date, 1, 's');
end;
format date date9.;
run;
Data All_Days_tbl;
SET All_Days_tbl_;
path_to_Check=CATS('"',"/usr/local/SAS/offers_daily_files/Loans.recomend.daily.",put(date,YYMMDDn8.),'"');
Run;
data wanted;
set All_Days_tbl;
IF fileexist(path_to_Check) then Ind=1;else ind=0;
Run;
No need for any macro variables and only your result data set is needed:
data example; length path_to_Check $ 200; do date="01JAN2021"d to today(); path_to_Check=CATS('"',"/usr/local/SAS/offers_daily_files/Loans.recomend.daily.",put(date,YYMMDDn8.),'"'); ind = fileexist(path_to_Check); output; end; run;
The fileexist returns 1 for found and 0 for not found so the IF /Then/ construct is a waste of clock cycles.
The iterated do will be easier to follow instead of the DO WHILE and avoids any possible infinite loop from a typo involving the incrementing of the dates.
For most purposes formatted date values just make macro coding difficult. The main usage would be in text that people read such as Title, Footnote text or file names.
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!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.