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.
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!
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.