Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically Build SET Statement (Multiple Files)

Status
Not open for further replies.

mmignot

Programmer
Jul 16, 2001
44
US
Hello all,

I would like to dynamically build a set statement with multiple files. The catch is that the every month a new file is created, and the last part of these files contain the date in "YYMM" format. Here is an example of the data
step and the "set" statement:

Code:
  data all_prft;
      set prft_9803
          prft_9804
          prft_9805
          prft_9806
          prft_9807
          prft_9808
          prft_9809
          prft_9810
          prft_0504
          prft_0505		
          ;

  run
  ;

Again, every month the set statement has to be updated, adding the lastest monthly file. I think that the set statement cn be built using a macro, and somehow making the "YYMM" part of the file a variable, maybe reading the "YYMM" from a text file that can be manually updated with the most recent month "YYMM" value?

Can anyone help with this?

Many thanks,
Mark :)
 
You can do this task using several different approaches.
1) If the monthly file is saved in a standard location you can have the full list of files in that place be put into your set statement via the macro engine.

This is how I would do this task
a- first get a list of all SAS datasets in your folder.
b- generate macro vars
c- create the set statement
(This works for windows, but I believe that someone could do this type of code for a Unix o/s as well.)

Code:
%macro setnms;
%*** SETUP THE PIPE ***;
filename getnms pipe 'dir c:\YOURSOURCE\*.sas7bdat /b /s';
%*** MAKE A SAS LIB TO THE SOURCE DATA ***;
libname t "c:\YOURSOURCE";

%*** GET THE SAS DATASET NAMES AND THEN SET THEM AS MACRO VARS ***;
data Dnames;
 infile getnms lrecl=2000;
 length mysasds  $200;
 input mysasds &;
 mysasds = scan(scan(mysasds,-1,'\'),1,'.');
run;
data _null_;
  set dnames end=last;
  call symput('ds'||trim(left(put(_n_,8.))), trim(mysasds) );
  if last then
    call symput('lim',trim(left(put(_n_,8.))) );
run;

%*** NOW STACK THEM ***;
data combined;
set
%do i=1 %to &lim;
 t.&&ds&i
%end;
;

run;
%mend;
%setnms;
I hope this helps you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top