13

I'm trying to concatenate multiple datasets in SAS, and I'm looking for a way to store information about individual dataset names in the final stacked dataset.

For eg. initial data sets are "my_data_1", "abc" and "xyz", each with columns 'var_1' and 'var_2'.

I want to end up with "final" dataset with columns 'var_1', 'var_2' and 'var_3'. where 'var_3' contains values "my_data_1", "abc" or "xyz" depending on from which dataset a particular row came.

(I have a cludgy solution for doing this i.e. adding table name as an extra variable in all individual datasets. But I have around 100 tables to be stacked and I'm looking for an efficient way to do this.)

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
steadyfish
  • 847
  • 2
  • 12
  • 27

2 Answers2

34

If you have SAS 9.2 or newer you have the INDSNAME option http://support.sas.com/kb/34/513.html

So:

data final;
format dsname datasetname $20.; *something equal to or longer than the longest dataset name including the library and dot;
set my_data_1 abc xyc indsname=dsname;
datasetname=dsname;
run;
Joe
  • 62,789
  • 6
  • 49
  • 67
10

Use the in statement when you set each data set:

data final;
 set my_data_1(in=a) abc(in=b) xyc(in=c);
 if a then var_3='my_data_1';
 if b then var_3='abc';
 if c then var_3='xyz';
run;
itzy
  • 11,275
  • 15
  • 63
  • 96