Wednesday, December 1, 2010

using macro to merge unknown number of sas data sets by a common variable

For example, you have many data sets as DATA1, DATA2, DATA3, ... , DATAn, and n is unknown. All these data sets have one common variable COMMON. Now you need to merge these data sets by COMMON.


data data1;
  input common a1;
  cards;
  2 3
  3 4
  4 5
  ;
  run;

data data2;
  input common a2;
  cards;
  21 3
  31 4
  41 5
  ;
  run;

%macro combineall (n);
  data combine;
   merge
   %do i=1 %to &n;
     DATA&i
   %end;  /* This ; is for end */
   ;      /* This ; is for merge */
         by COMMON;
%mend;

%allcombine(2)

proc print data=combine;
run;

1 comment:

  1. Very Impressive SAS Programming tutorial. The content seems to be pretty exhaustive and excellent and will definitely help in learning SAS Programming. I'm also a learner taken up SAS Tutorial and I think your content has cleared some concepts of mine. While browsing for SAS Tutorial on YouTube i found this fantastic video on SAS Programming. Do check it out if you are interested to know more.:-https://www.youtube.com/watch?v=4PtMFE4IjLg&t=456s

    ReplyDelete