how to read latest csv file from a directory present in unix using sas code
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;
I think this might be the answer
Latest by modification timestamp, or by a timestamp coded into the filename?
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;
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:
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;
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.
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.