BookmarkSubscribeRSS Feed
bncoxuk
Obsidian | Level 7

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.

6 REPLIES 6
art297
Opal | Level 21

data all;

  set data2:;

run;

bncoxuk
Obsidian | Level 7

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.

Ksharp
Super User

Your files are some txt files or datasets of sas ?

Ksharp

ballardw
Super User

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);

Peter_C
Rhodochrosite | Level 12

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 ;

ballardw
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 760 views
  • 3 likes
  • 5 in conversation