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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1146 views
  • 2 likes
  • 3 in conversation