I have a folder where a new file is put every 2 weeks and at start of the month i have to pull the last three files from that folder into a sas code.
The data is never the same there are mostly 2 files in one and i need to pull a file from the month before that. what the datafiles look like in my folder. Currently the folder has these files
outreach_201030.sas7bdat
outreach_201113.sas7bdat
outreach_201127.sas7bdat
outreach_201211.sas7bdat
outreach_201225.sas7bdat
For example in Jan 2021 I would need to pull the last file. Which are the two file from Dec 2020 and the last file from Nov 2020.
outreach_201127.sas7bdat
outreach_201211.sas7bdat
outreach_201225.sas7bdat
Is there an automated way to pull these files without me having to go in and changing the dates manually.
If your files are strictly sas datasets, the following will work:
libname MYLIB "FOLDER_PATH_HERE";
proc sql outobs=3;
create table want as
select memname
from dictionary.tables
where libname='MYLIB' and memtype='DATA' and memname like 'OUTREACH%'
order by memname desc;
quit;
Establish a library MYLIB using your folder location.
Use DICTIONARY.TABLES to list all tables within MYLIB.
Order by descending memname so that the most recent files are first.
Use the OUTOBS option to limit the output to 3 records.
From here, you can place the results into macro variables (FILE_1, FILE_2, FILE_3) by doing the following:
proc sql noprint;
select memname into :FILE_1 - :FILE_3
from want;
quit;
%put &=file_1 &=file_2 &=file_3;
Now the names of the datasets can be accessed with &FILE_1, &FILE_2, &FILE_3
If your files are strictly sas datasets, the following will work:
libname MYLIB "FOLDER_PATH_HERE";
proc sql outobs=3;
create table want as
select memname
from dictionary.tables
where libname='MYLIB' and memtype='DATA' and memname like 'OUTREACH%'
order by memname desc;
quit;
Establish a library MYLIB using your folder location.
Use DICTIONARY.TABLES to list all tables within MYLIB.
Order by descending memname so that the most recent files are first.
Use the OUTOBS option to limit the output to 3 records.
From here, you can place the results into macro variables (FILE_1, FILE_2, FILE_3) by doing the following:
proc sql noprint;
select memname into :FILE_1 - :FILE_3
from want;
quit;
%put &=file_1 &=file_2 &=file_3;
Now the names of the datasets can be accessed with &FILE_1, &FILE_2, &FILE_3
Note that MEMNAME field is always in uppercase.
libname='MYLIB' and memname like 'OUTREACH^_%' escape '^'
Good catch, thanks Tom!
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!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.