Hello,
I have a data directory/folder that is updated monthly with the same naming convention each month. The only thing that changes in the directory name and path is the date at the end of the directory name. Unfortunately, that date is not consistent and changes month to month (e.g. not always on the 2nd day of the month, etc., so I can't use a date macro). Is it possible to assign a libname to a partial directory name (e.g. just to "C:\monthlydata\CCMonthlyRun")? That way I wouldn't have to update the date every time I need to reference that library?
Example of current lib statement:
libname TestX "C:\monthlydata\CCMonthlyRun[11.04.22]";
I assume that you mean that the name of the DIRECTORY that houses the file changes (and not the name of the file which is just based on the name of the dataset).
This is easy to get a list of files and directories in a file. The simplest is if you can just ask the operating system to tell you. Since it looks like you are using Windows let's use the DIR command.
data files;
infile "dir ""C:\monthlydata\"" /b" pipe;
input filename $256. ;
if filename=:'CCMonthlyRun';
run;
If you cannot use the PIPE engine then check out the DOPEN() and DREAD() functions.
Once you have the list then parse out the date part of the name and convert it into an actual DATE value.
data dates;
set files;
date = input(substr(filename,length('CCMonthlyRun')+1),anyddte.);
format date yymmdd10.;
run;
Then you can apply some logic to decide which of the names you want to use. Perhaps the latest one?
proc sort data=dates;
by date;
run;
And once you have picked one then you can assign that value to a macro variable.
data _null_;
set dates end=eof;
if eof then call symputx('dirname',filename);
run;
Which you can use in your code.
libname textx "C:\monthlydata\&dirname";
I'm not sure I know what you want.
Can you show us the actual way things are now and clearly point out the problem, and how you envision things would be if you fixed it? Please use the Insert Photos icon to include screen captures in your reply. Do not attach files.
I assume that you mean that the name of the DIRECTORY that houses the file changes (and not the name of the file which is just based on the name of the dataset).
This is easy to get a list of files and directories in a file. The simplest is if you can just ask the operating system to tell you. Since it looks like you are using Windows let's use the DIR command.
data files;
infile "dir ""C:\monthlydata\"" /b" pipe;
input filename $256. ;
if filename=:'CCMonthlyRun';
run;
If you cannot use the PIPE engine then check out the DOPEN() and DREAD() functions.
Once you have the list then parse out the date part of the name and convert it into an actual DATE value.
data dates;
set files;
date = input(substr(filename,length('CCMonthlyRun')+1),anyddte.);
format date yymmdd10.;
run;
Then you can apply some logic to decide which of the names you want to use. Perhaps the latest one?
proc sort data=dates;
by date;
run;
And once you have picked one then you can assign that value to a macro variable.
data _null_;
set dates end=eof;
if eof then call symputx('dirname',filename);
run;
Which you can use in your code.
libname textx "C:\monthlydata\&dirname";
Thank you, this worked like a charm!
You may want to consider the implications of changing from a folder per month approach to storing your SAS data, to just having one folder where the dataset name suffix identifies the month of the data (MyDataset_20220411). Then your LIBNAME problem disappears and SAS library maintenance becomes much more efficient.
On top of what everyone else said, never use 2-digit years (this should be a total given after Y2K!), and use dates in YMD notation, so the filenames sort correctly on their own.
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
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.