DATA Step, Macro, Functions and more

How to write generic libname to multiple databases?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 125
Accepted Solution

How to write generic libname to multiple databases?

[ Edited ]

I'd like to stack 20 different datasets in data step. They have common libname. How to specify common libname for 20 or more different datasets? to execute concantenation as shown below?  Listing libname for each dataset would become quite tedious task.  

 

data libname2.want; set libname2(com2011Jan16 through com2017Feb29); 

I appreciate your time. 

 


Accepted Solutions
Solution
‎07-09-2017 06:24 PM
Respected Advisor
Posts: 3,893

Re: How to write generic libname to multiple databases?

@Cruise

Looks to me like you're almost there and it's just about using correct SAS syntax - remove the bracket around the SAS table name.

data want; set home.maggiechildren:;

 

Here some working sample code:

/* libname to folder where "maggiechildren:" ds are stored */
libname home (work);
/* libname home '<path to your SAS folder>'; */ /* prepare test data */ data maggiechildren2017Jun23 maggiechildren2012Jan11 ; set sashelp.class; run; /* read all maggiechildren into single table */ data want; length _sourceTable sourceTable $41; format data_date date9.; set home.maggiechildren: indsname=_sourceTable; sourceTable=_sourceTable; data_date=input(substr(_sourceTable,length(_sourceTable)-8),?? anydtdte.); run;

View solution in original post


All Replies
Valued Guide
Posts: 765

Re: How to write generic libname to multiple databases?

Hi, if it's all the data sets in that library, here's an example ...


libname z 'z:\';

 

data z.one;
retain x 10;
run;

 

data z.two;
retain x 10;
run;

 

* concatenate the data set names into a macro variable;

proc sql;
select catt('z.',memname) into :dsets separated by ' '
from dictionary.tables where libname = 'Z';
quit;

 

data new;
set &dsets;
run;

 

If your data sets had a common numeric suffix, it's easier ...


data z.d1;
retain x 10;
run;

 

data z.d2;
retain x 10;
run;


data new;
set z.d1-z.d2;
run;

Frequent Contributor
Posts: 125

Re: How to write generic libname to multiple databases?

wow, thank you, but lot more work than I thought. my initial reaction is to get back to exhaustive iterations.
PROC Star
Posts: 1,561

Re: How to write generic libname to multiple databases?

[ Edited ]

Like this?

%macro list_tables(start=19991231,end=19991231);
  %local daten suffix i; 
  %let daten=%sysfunc(inputn(&start,yymmdd8.));
  %do %while(&daten<=%sysfunc(inputn(&end,yymmdd8.)));
    %let suffix=%sysfunc(putn(&daten,yymon7.))%sysfunc(day(&daten));
LIBNAME2.COM&suffix
    %let daten=%sysfunc(intnx(day,&daten,1));
  %end;
%mend;
                              
data libname2.want; 
  set %list_tables(start=20160128,end=20160203); 
run;

set LIBNAME2.COM2016JAN28 LIBNAME2.COM2016JAN29 LIBNAME2.COM2016JAN30 LIBNAME2.COM2016JAN31
LIBNAME2.COM2016FEB1 LIBNAME2.COM2016FEB2 LIBNAME2.COM2016FEB3

You are asking for trouble with these suffixes.

20171231 will be easier to manage in the long run.

Frequent Contributor
Posts: 125

Re: How to write generic libname to multiple databases?

goodness gracious. i'd simply list the libname 20 times for each datasets :-)
PROC Star
Posts: 1,561

Re: How to write generic libname to multiple databases?

Maybe this is what you want then:

 

  set LIBNAME2.COM2016JAN16-LIBNAME2.COM2016JAN31   LIBNAME2.COM2016FEB:  ;

Respected Advisor
Posts: 3,893

Re: How to write generic libname to multiple databases?

@SUNY_Maggie

May be we just all missunderstand what you're really dealing with and you need to provide additional information first. Let me ask you a few questions to give you an idea what we probably need to know to better understand your situation.

 

1. Are these SAS tables or tables in one or multiple databases/database schemas?

2. Do the tables all have the same structure (same column names with the same attributes like type, lenght, format...)?

3. If SAS tables: Are these tables stored in a single folder or in multiple folders?

4. If SAS tables in multiple folders: 

    a) Are the names of these SAS tables identical or do they differ

    b) If the names are different: Do the names follow some naming convention and if yes, what's the naming convention

 

 

 

Frequent Contributor
Posts: 125

Re: How to write generic libname to multiple databases?

@Patrick

 

I'm dealing with multiple databases with same exact column names with the same attributes like type, lenght, format. It's a hospital discharge data periodically updated. Each update is output to separate sas data and stored in the single folder. Datasets are labeled to contain constant initials followed with date when created.

i.e.

maggiechildren2017Jun23

maggiechildren2012Jan11

 

data want; set home.(maggiechildren:);

I tried as shown in the code taking advantage of initial part of the name they all share with no success.

 

Thanks for asking for a clarification.

 

Solution
‎07-09-2017 06:24 PM
Respected Advisor
Posts: 3,893

Re: How to write generic libname to multiple databases?

@Cruise

Looks to me like you're almost there and it's just about using correct SAS syntax - remove the bracket around the SAS table name.

data want; set home.maggiechildren:;

 

Here some working sample code:

/* libname to folder where "maggiechildren:" ds are stored */
libname home (work);
/* libname home '<path to your SAS folder>'; */ /* prepare test data */ data maggiechildren2017Jun23 maggiechildren2012Jan11 ; set sashelp.class; run; /* read all maggiechildren into single table */ data want; length _sourceTable sourceTable $41; format data_date date9.; set home.maggiechildren: indsname=_sourceTable; sourceTable=_sourceTable; data_date=input(substr(_sourceTable,length(_sourceTable)-8),?? anydtdte.); run;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 150 views
  • 3 likes
  • 4 in conversation