Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAS combine 3 datasets and assign its corresponding dataset name [duplicate]

Tags:

sas

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;
like image 723
useR Avatar asked Dec 05 '25 13:12

useR


2 Answers

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;
like image 149
in_user Avatar answered Dec 07 '25 14:12

in_user


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.

like image 28
Leo Avatar answered Dec 07 '25 13:12

Leo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!