Help using Base SAS procedures

How to combine files with set

Reply
Frequent Contributor
Posts: 131

How to combine files with set

I have files with names defined by year and month. Essentailly, these files have exactly the same variables.

The file names are:

data200906

data200907

data200908

data200909

data200910

data200911

data200912

data201001

data201002

data201003

......

data201107

data201108

Can you please teach me how I can combine these files in a single data step?

Thanks a lot in advance.

PROC Star
Posts: 7,489

How to combine files with set

data all;

  set data2:;

run;

Frequent Contributor
Posts: 131

How to combine files with set

Oh dear, that is really a magic Smiley Happy Thanks a lot.

If I want to use some simple macro plus DO statement, how to achieve this? Just interested.

Super User
Posts: 10,044

How to combine files with set

Your files are some txt files or datasets of sas ?

Ksharp

Super User
Posts: 11,343

How to combine files with set

Since the %DO isn't allowed in open code you need a macro. The example below allows parameters to change the start and end year. It could be made more generic with a parameter for the base of the data set name as well. Pay attention to the positions of semicolons!.

%macro dname(syr=,eyr=);

   data all;

      set

     %do yr=&syr %to &eyr;

       %do m=1 %to 12;

           %let m2= %sysfunc(putn(&m,z2.));/* this is to get the leading zeros*/

                                           /* the %DO will not increment leading 0*/

           data&yr.&m2

       %end;

     %end;

   ;/* to close set statement*/

      /* other code*/

    run;

%mend;

/* how to use*/

%dname(syr=2009, eyr=2011);

Valued Guide
Posts: 2,177

How to combine files with set

bncoxuk wrote:

Oh dear, that is really a magic Thanks a lot.

If I want to use some simple macro plus DO statement, how to achieve this? Just interested.

Since the DATA: approach became available only with SAS9.2, the macro approach and the follow method would be needed for earlier releases.

filling a macro variable with a list of the data set names is fairly straight forward :

proc sql noprint ;

select trim( libname) || '.' || memname into :names separated by ' '

from dictionary.members

where libname='YOURLIB' and memname like 'DATA______'

order by 1 ;

quit ;

* and used like ;
data together ;

   set &names ;

run ;

Super User
Posts: 11,343

How to combine files with set

Peter's solution is better than mine in that you won't reference any non-existant data sets. Mine was just to show one approach using an explicitly iterated %DO loop using the obvious pattern shown in the data set names.

Ask a Question
Discussion stats
  • 6 replies
  • 183 views
  • 3 likes
  • 5 in conversation