04-03-2014 03:30 AM
Never coded myself SAS macro before.
I have a list of text files in a Linux directory with the name convention like below:
I hope to extract the date first from all the text files within in that directory say /path/to/file/
and then feed the date just created into the dt macro variable below and repeat the process as below for all the files in that directory. Could you please tell me how to best code the macro?
%let dt = 20111004;
key $ 15
create table want.xwant_&dt as
select distinct date, key
I know that if the files in the directory are all SAS then it might be easier...
set abccont end=last;
if last then
%do i=1 %to &count;
04-03-2014 05:05 AM
As for getting the date from the filename, something like this should work:
filename dir_list pipe 'dir "c:\daten" /B /s /tw';
attrib buffer format=$2000. file_date format=date9.;
input buffer $2000.;
The dataset temp will have filename and date variables. However I am not sure on your second question though. Do you want to put these dates into their own separate macro variables? This doesn't seem like an efficient way of doing things, and if the are lots of files that would mean lots of macro variables. Another way of doing it is to use the file and date list as before and let call execute generate the code from there:
call execute('proc sql;
create table want.xwant_'||put(file_date,date9.)||' as
04-03-2014 06:28 AM
Do you want to read the data from all the files in the directory?
If so do all the files in the directory have the same record layout? That is can the fields all be read with the same INPUT statement(s)?
04-03-2014 10:50 AM
Reading the files can be done by using wildcards in a filename statement
filename readit "<system path>/*.txt";
The use the readit or whatever you use in the filename in the infile statement.
04-03-2014 07:38 AM
Where is Tom? This is almost the same question as in: https://communities.sas.com/thread/56245
You could use pipe and "ls ---" commands using the X-cmd access but the SAS functions are probably nicer.