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

Hello!!

I am quite new in SAS programming, so I have some doubts how to merge dynamic datasets.

I have a table containing a variable with the names of multiple datasets I would like to merge into one single dataset.

I would like to know how I can merge all these datasets. All of them are placed in the same library.

 

Thank you!!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@Sica wrote:
Sorry, I would like to append.

All the tables have the same columns. The library has more than 1000 datasets, and I would like to append around 80 of them. So I created a variable containing all the datasets I would like to append, but I do know how to it dynamically, only by typing dataset by dataset =/
I would like to do it in a smarter way.

Thank you.

It isn't clear what you mean by a "variable containing all the datasets".

If you mean that you have a dataset containing the names you want as one observation per name then something like this will work if there are not problems with variable properties the same. Note, not efficient but simple code:

 

proc sql;
   select datasetnames into : namelist separated by ' '
   from datasetwithnames
   ;
run;

data want;
   set &namelist.;
run;

Or if the names are "nice" such as common base name ending with sequence number you could use a list like

 

data want;
   set dsname1 - dsname10;
run;

 There are other ways to build lists depending on the values. It might help to show the names you are concerned with so we don't have to make a lot of guesses.

View solution in original post

10 REPLIES 10
Reeza
Super User
Is the merge complicated or just a simple BY statement?

proc sql noprint;
select name into :merge_list separated by " " from listDatasets;
quit;

data want;
merge &merge_list;
by commonVariable;
run;
ballardw
Super User

First question: Merge or Append?

Merge in SAS usually refers to "horizontal" combining such as matching on a value.

Append would mean "add to the end of" an existing data set.

 

Second: Do variables of the same names have the same properties? If variable X is not numeric or character (either one) in all of the sets you will have a failure. If common variables are character you can get truncated data.

 

Best is provide some examples of the data and what the result should be.

Sica
Fluorite | Level 6
Sorry, I would like to append.

All the tables have the same columns. The library has more than 1000 datasets, and I would like to append around 80 of them. So I created a variable containing all the datasets I would like to append, but I do know how to it dynamically, only by typing dataset by dataset =/
I would like to do it in a smarter way.

Thank you.
ballardw
Super User

@Sica wrote:
Sorry, I would like to append.

All the tables have the same columns. The library has more than 1000 datasets, and I would like to append around 80 of them. So I created a variable containing all the datasets I would like to append, but I do know how to it dynamically, only by typing dataset by dataset =/
I would like to do it in a smarter way.

Thank you.

It isn't clear what you mean by a "variable containing all the datasets".

If you mean that you have a dataset containing the names you want as one observation per name then something like this will work if there are not problems with variable properties the same. Note, not efficient but simple code:

 

proc sql;
   select datasetnames into : namelist separated by ' '
   from datasetwithnames
   ;
run;

data want;
   set &namelist.;
run;

Or if the names are "nice" such as common base name ending with sequence number you could use a list like

 

data want;
   set dsname1 - dsname10;
run;

 There are other ways to build lists depending on the values. It might help to show the names you are concerned with so we don't have to make a lot of guesses.

Sica
Fluorite | Level 6
Thank you a lot!!

Is there an easy way to create a column and add the original file name from all values?
Reeza
Super User
INDSNAME option on the SET statement + save it.

data want;
set ..... INDSNAME = source;
dataSource = source;
run;
Sica
Fluorite | Level 6
Reeza, thanks a lot! It woks!! =o)
ballardw
Super User

@Sica wrote:
Thank you a lot!!

Is there an easy way to create a column and add the original file name from all values?

The Set statement option INDSNAME name will do that. The default behavior is to create a temporary variable, so you need to assign that value to a desired variable.

Example:

data junk;
   set sashelp.class indsname=ds;
   source=ds;
run;

DS is a temporary variable that holds the library and dataset name and will not be written to the output data set (that's what temporary means). Source is assigned the value of the variable DS and will be kept in the data. You only use the option one time and it really doesn't matter where it goes on the set statement.

 

 

 

Reeza
Super User

Naming conventions would make this trivial so I'd highly recommend you pick a good naming convention. 

Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

 

And a slight modification to @ballardw code in case you don't have the library included in the list of data set names.

 

%let myLibname = sashelp;
proc sql;
   select catx(".", "&myLibname.", datasetnames) into : namelist separated by ' '
   from datasetwithnames
   ;
run;

data want;
   set &namelist.;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 10 replies
  • 1476 views
  • 6 likes
  • 4 in conversation