BookmarkSubscribeRSS Feed
aashi77
Calcite | Level 5

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.

4 REPLIES 4
Patrick
Opal | Level 21

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;
aashi77
Calcite | Level 5

By latest, I mean the file which was last modified.

Tom
Super User Tom
Super User

@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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 927 views
  • 0 likes
  • 4 in conversation