BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
4 REPLIES 4
David_SAS
SAS Employee
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
deleted_user
Not applicable
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. 🙂
deleted_user
Not applicable
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.
deleted_user
Not applicable
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 4 replies
  • 1983 views
  • 0 likes
  • 2 in conversation