BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Crt
Fluorite | Level 6 Crt
Fluorite | Level 6
Hi everyone!
I am trying to read the latest file from a folder.

Requirements:
1) The folder stores files as:
log_26112021 123456.log

2) The file format is the name_date numbers. There is a space between the date and the numbers.

What I have done so far is:
filename log "&path.\log_%sysfunc(today(), ddmmyyN8.).log";

What I want to do is to read the rest of the file name, whatever it is including the space and the numbers. Any suggestions?

Thank you in advance!
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You can use a wildcard in the filename.

You could just try to read ALL of the files that match the pattern.  You can use the FILENAME= option on the INFILE statement to find the name of the file you are reading.

data logs;
  length fname filename $200;
  infile "&path./log_%sysfunc(today(), ddmmyyN8.)*.log" filename=fname ;
  input;
  filename=fname;
  lineno+1;
  if filename ne lag(filename) then lineno=1;
  line=_infile_;
run;

View solution in original post

4 REPLIES 4
Reeza
Super User
  • Mismatched quotes
  • * is the wildcard character
  • I'm unsure of the behaviour of this code if it resolves to multiple files

 

filename log "&path.\log_%sysfunc(today(), ddmmyyN8.) *.log";

@Crt wrote:
Hi everyone!
I am trying to read the latest file from a folder.

Requirements:
1) The folder stores files as:
log_26112021 123456.log

2) The file format is the name_date numbers. There is a space between the date and the numbers.

What I have done so far is:
filename log "&path.\log_%sysfunc(today(), ddmmyyN8.).log';

What I want to do is to read the rest of the file name, whatever it is including the space and the numbers. Any suggestions?

Thank you in advance!

 

Crt
Fluorite | Level 6 Crt
Fluorite | Level 6
Thank you for your response.

I have tried the * character, but it seems not to work.
Reeza
Super User
What do you mean 'it seems not to work'.
What are you expecting to happen and what happens?
How are you using the filename and how does it not work?
Code and log helps.
Tom
Super User Tom
Super User

You can use a wildcard in the filename.

You could just try to read ALL of the files that match the pattern.  You can use the FILENAME= option on the INFILE statement to find the name of the file you are reading.

data logs;
  length fname filename $200;
  infile "&path./log_%sysfunc(today(), ddmmyyN8.)*.log" filename=fname ;
  input;
  filename=fname;
  lineno+1;
  if filename ne lag(filename) then lineno=1;
  line=_infile_;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2297 views
  • 1 like
  • 3 in conversation