BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Cruise
Ammonite | Level 13

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. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@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

8 REPLIES 8
MikeZdeb
Rhodochrosite | Level 12

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;

Cruise
Ammonite | Level 13
wow, thank you, but lot more work than I thought. my initial reaction is to get back to exhaustive iterations.
ChrisNZ
Tourmaline | Level 20

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.

Cruise
Ammonite | Level 13
goodness gracious. i'd simply list the libname 20 times for each datasets 🙂
ChrisNZ
Tourmaline | Level 20

Maybe this is what you want then:

 

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

Patrick
Opal | Level 21

@Cruise

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

 

 

 

Cruise
Ammonite | Level 13

@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.

 

Patrick
Opal | Level 21

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 8 replies
  • 1447 views
  • 3 likes
  • 4 in conversation