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

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]";

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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";

 

 

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Reeza
Super User
You can scan the folder for filenames and find the most recent file updated. Search on here to find that question answered almost every month.

If your file naming convention has the day part only changing you could also wildcard that part of the name only.

Tom
Super User Tom
Super User

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";

 

 

bananah13
Fluorite | Level 6

Thank you, this worked like a charm!

SASKiwi
PROC Star

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.

Kurt_Bremser
Super User

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.

sas-innovate-white.png

Missed SAS Innovate in Orlando?

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.

 

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1626 views
  • 3 likes
  • 6 in conversation