DATA Step, Macro, Functions and more

%macro for merge

Reply
New Contributor
Posts: 3

%macro for merge

Hi All,

My requirement here is to merge 'n' number of datasets in a library using a sas macro as that seems to be the feasible solution.The sas datasets' names in the library are all different i.e.,does not start with the same prefix. all the datasets have a common variable which is date and the datasets have to be merged based on date. The data in the datasets are already sorted. I tried using proc sql to first gather all the variables in a macro variable. From there on how should the merge be accomplished?

Regular Contributor
Posts: 195

Re: %macro for merge

The requirement sounds like homework. You don't need the names of the variables to merge the datasets. You need the names of the datasets in the library - the dataset sashelp.vtable holds that information.

 

proc sql noprint;
  select catx(".", LibName, MemName)
    into :datasetList separated by " "
    from sashelp.vtable
      where LibName = "YOUR_LIB_IN_UPCASE"
  ;
quit;

data work.merged;
  merge &datasetList.;
  by Date;
run;
Super User
Super User
Posts: 9,193

Re: %macro for merge

I just want to add that in a real life situation you wouldn't do this mass merge.  It is fraught with unseen dangers, such as variables being in different tables, different types, many to many merges etc.  Normally you should know your data well, and be able to pick out data items only when needed.

Ask a Question
Discussion stats
  • 2 replies
  • 76 views
  • 2 likes
  • 3 in conversation