2

I have 3 dataset as shown:

data dataA;
    input id;
    datalines;
1001
1002
1003
;
run;

data dataB;
    input id;
    datalines;
1001
1002
1003
;
run;

data dataC;
    input id;
    datalines;
1001
1002
1003
;
run;

i would like to have output:

1001 dataA
1002 dataA
1003 dataA
1001 dataB
1002 dataB
1003 dataB
1001 dataC
1002 dataC
1003 dataC

I know how to combine three data sets using

data datacombine;
  set dataA dataB dataC;
run;
useR
  • 3,062
  • 10
  • 51
  • 66

2 Answers2

7

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

data datacombine;
format dsname datasetname $25.; 
  set dataA dataB dataC indsname=dsname;
  datasetname=dsname;
run;
in_user
  • 1,948
  • 1
  • 15
  • 22
  • Joe has beautifully explained it alrady in some other post http://stackoverflow.com/questions/12144138/sas-concatenate-different-datasets-while-keeping-the-individual-data-table-name/12161432#12161432 this marks your question as duplicate!! – in_user Jan 06 '15 at 04:40
  • Good point, I've closed as duplicate - feel free to flag to close as well in the future (I think you can do that at your rep level?). – Joe Jan 06 '15 at 15:26
2

I think this is the most standard way of doing it:

data datacombine;
   set dataA(in=A) dataB(in=B) dataC(in=C);
   if      A then origin = "dataA";
   else if B then origin = "dataB";
   else if C then origin = "dataC";
run;

The in= dataset option sets a flag if the row originated from the corresponding input dataset.

Leo
  • 2,775
  • 27
  • 29