The SAS Output Delivery System and reporting techniques

Dynamic Libname?

Not applicable
Posts: 0

Dynamic Libname?

Hello All,

I'm trying to find a shorcut around the following. I'm producing a sales report that spans several months, the months will change depending on client request, but the data is structured so each months purchases are on a different shown below by my current Libname:

libname a '/data/2006/05/';
libname b '/data/2006/06/';
libname c '/data/2006/07/';

I then pull these with a quick datastep

data one;

Is there a short cut or any way of creating a macro to allow me to pick a date range so I won't have to hard code more libnames with each passing month? Thanks!

SAS Employee
Posts: 174

Re: Dynamic Libname?

Posted in reply to deleted_user

This question is perhaps best answered by Technical Support. You can submit it online at .

-- David Kelley, SAS
Not applicable
Posts: 0

Re: Dynamic Libname?

Posted in reply to deleted_user
I don't think to be smarter than Technical Support, but I was thinking that perhaps you could use a macro, like the following:

%macro SalesRepPerMonth(StartMonth,StopMonth);
%local i;
* Create empty dataset;
proc sql;
create table work.SalesRepTot(Month char(2));
* Loop over the months and simultaneously fill the variable Month;
%do i = &StartMonth %to StopMonth;
%put i = *i.; * Show the value of i in the Log;
libname a "data/2006/&i./";
data work.SalesRepTot; set work.SalesRepTot a.MyMonthFile;
Month = &i.;
libname a clear;

This macro is easy to expand. It could include year as parameter. Or you could include a call to another macro which would create the actual sales report (as a HTML-file?). Hope this helps. Smiley Happy
Not applicable
Posts: 0

Re: Dynamic Libname?

Posted in reply to deleted_user
A libname can be something of the form:

LIBNAME x ('c:\dir1', 'c:\dir2', ..., 'c:\dirn');
So: LIBNAME x ('c:\data\2006\05', 'c:\data\2006\06', 'c:\data\2006\07');

Or you do something like:

LENGTH dir dirs $ 300;
DO yr = 2001 TO 2003; * year directory ;
DO m = 1 TO 12; * month directory;
dir = Quote(Trimn('c:\data\' ||Put(yr,4.0)||'\'||Put(m,z.2)));
IF dirs EQ '' THEN dirs = Trimn(dir);
ELSE dirs = Trimn(dirs) || ',' ||Trimn(dir) ;
Call SymPut('mdirs', dirs);

LIBNAME f (&mdirs);

SET f.file1 f.file2 f.file3;

Haven't tested this so you might have to spruce it up little, but you should get the idea. In particular, you could adapt the above to read inline data.

Depending on the version of SAS you're running, you can also put the name of file directly. But that's another time. Or homework.
Not applicable
Posts: 0

Re: Dynamic Libname?

Posted in reply to deleted_user
That works where you have differently named tables in the libraries making up the concatenated assignment, but the correspondent specifically asked for different libraries and tables of the same name.

The rules for accessing objects in concatenated libraries will apply, and only the table from the first library will be surfaced.

Which is a shame because it does present an otherwise neat solution.

Kind regards

Ask a Question
Discussion stats
  • 4 replies
  • 2 in conversation