The SAS Output Delivery System and reporting techniques

Dynamic Libname?

Reply
N/A
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 dataset...as 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;
set
a.file
b.file
c.file
;
run;

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!

Jose
SAS Employee
Posts: 174

Re: Dynamic Libname?

Jose,

This question is perhaps best answered by Technical Support. You can submit it online at http://support.sas.com/ctx/supportform/index.jsp .

-- David Kelley, SAS
N/A
Posts: 0

Re: Dynamic Libname?

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));
quit;
* 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.;
run;
libname a clear;
%end;
%mend;
%SalesRepPerMonth(01,12);

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
N/A
Posts: 0

Re: Dynamic Libname?

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:

DATA _NULL_;
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) ;
END;
END;
Call SymPut('mdirs', dirs);
RUN;

LIBNAME f (&mdirs);

DATA x;
SET f.file1 f.file2 f.file3;
RUN;

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.
N/A
Posts: 0

Re: Dynamic Libname?

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

David
Ask a Question
Discussion stats
  • 4 replies
  • 726 views
  • 0 likes
  • 2 in conversation