BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sandhya55
Obsidian | Level 7

how to read latest csv file from a directory present in unix using sas code

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

First, replace the PUT with the CALL SYMPUTX:

filename file1 pipe "cd /folder/sub-folder ;ls -t | head -1" ;

data _null_;
infile file1 truncover ;
input filename $100.;
call symputx('fname',filename);
run;

Then, define the target library, andv' read the file into a dataset:

libname file_ext '/folder/sub-folder/file_ext' ;

data file_ext.file2_Read ;
infile "/folder/sub-folder/&fname." dlm="," dsd truncover;
input
  /* insert variables to be read */
;
run;

View solution in original post

9 REPLIES 9
Sandhya55
Obsidian | Level 7

i have used below script to get the latest csv file in directory :

 

but the script is not working for me .can any one please help me resolve the issue.

filename file1 pipe "cd /folder/sub-folder ;ls -t | head -1" ;
libname file_ext 'cd /folder/sub-folder/file_ext' ;

data file_ext.file2_Read ;

infile file1 truncover ;
input filename $100 ;
put filename=;
/*
call symputx('lastfile',file1);
*/
run;

Sandhya55
Obsidian | Level 7
Latest by modification timestamp
Kurt_Bremser
Super User

Your LIBNAME won't work; you can't use a UNIX command as a path

Simplify your code first to see if you get the desired file.

filename file1 pipe "cd /folder/sub-folder ;ls -t | head -1" ;

data _null_;
infile file1 truncover ;
input filename $100.;
put filename=;
run;

Read the log to see the filename; if you have issues, please copy/paste your log into a window opened with this button:

Bildschirmfoto 2020-04-07 um 08.32.59.jpg

Sandhya55
Obsidian | Level 7
Thanks !!! latest file name is listed in filename .Can you please provide the syntax to read this file as we are dynamically getting the file name from directory
Kurt_Bremser
Super User

First, replace the PUT with the CALL SYMPUTX:

filename file1 pipe "cd /folder/sub-folder ;ls -t | head -1" ;

data _null_;
infile file1 truncover ;
input filename $100.;
call symputx('fname',filename);
run;

Then, define the target library, andv' read the file into a dataset:

libname file_ext '/folder/sub-folder/file_ext' ;

data file_ext.file2_Read ;
infile "/folder/sub-folder/&fname." dlm="," dsd truncover;
input
  /* insert variables to be read */
;
run;
Kurt_Bremser
Super User

You could further simplify the retrieval of the filename:

data _null_;
infile "ls -t /folder/subfolder/*.csv" pipe truncover ;
input filename $200.;
call symputx('fname',filename);
stop;
run;

The macro variable should have the complete path in it, so you don't need to explicitly code it in the next data step.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 9 replies
  • 2120 views
  • 2 likes
  • 3 in conversation