In the folder, there are 2 files: a1.csv and a2.csv
The part of the code is:
infile "/doc/a*.csv";
It is reading both files. I want only the latest one.
I assume "latest" means the file with the highest number.
You will need to first create a directory listing and then use some logic to find the the file with the highest number (and it's not how they sort alphabetically because a2.csv will sort after a10.csv).
Can you execute OS commands out of SAS? Run below syntax and let us know if you see in the log XCMD or NOXCMD.
proc options option=xcmd;
run;
Define "latest". Highest number in the name or latest modification timestamp of the file?
By latest, I mean the file which was last modified.
@aashi77 wrote:
In the folder, there are 2 files: a1.csv and a2.csv
The part of the code is:infile "/doc/a*.csv";
It is reading both files. I want only the latest one.
Do not use the * wildcard in the filename if you only want one file.
Which file is the latest one? How do you know (what is the logic you used to pick)? Can you convert that logic into code?
It is easy to get the list of files in a directory. For example see this macro: https://github.com/sasutils/macros/blob/master/dirtree.sas
But there are other ways also. You could even use your wildcard trick if you only need the NAME of the file to figure out which is the latest.
data filelist;
length fname filename $200;
infile "/doc/a*.csv" filename=fname;
input;
if fname ne lag(fname);
filename=fname;
run;
Once you have picked the right filename there are many ways to use that name in your step that actually reads the file.
You could put it into a macro variable.
data _null_;
set filelist;
call symputx('filename',filename);
run;
data want;
infile "&filename" dsd truncover firstobs=2;
... rest of step to read CSV file...
run;
Or skip the macro variable and just leave the name in a dataset and use the FILEVAR= option of INFILE statement to tell SAS which file to read.
data want;
if _n_=1 then set filelist;
infile dummy filevar=filename dsd truncover firstobs=2;
... rest of step to read CSV file...
run;
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.
Ready to level-up your skills? Choose your own adventure.