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

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. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
unison
Lapis Lazuli | Level 10

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

 

-unison

View solution in original post

3 REPLIES 3
unison
Lapis Lazuli | Level 10

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

 

-unison
Tom
Super User Tom
Super User

Note that MEMNAME field is always in uppercase.

libname='MYLIB' and memname like 'OUTREACH^_%' escape '^'
unison
Lapis Lazuli | Level 10

Good catch, thanks Tom!

-unison

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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