Carl
Carl

Reputation: 11

How to batch modify SAS programs with a SAS program?

I know we can use data steps to infile "*.sas" programs as a dataset, each line of code is one record of the dataset. And then I can make changes to the dataset with SAS.

Let's say I have 100 programs already exist in c:\pgm, all I need to do is minor modifications, such as changing flag1 to flag2 for all these 100 program. If I don't want to open each program and substitute the flag one by one. Is there a way to get all program names in c:\pgm, so that I can loop over these names and do the substitution.

I'm using SAS 9.4 and EG. Thanks!

Upvotes: 0

Views: 565

Answers (1)

Sale
Sale

Reputation: 349

I might have overcomplicated it a bit, but the idea is the following:

  • extract the list of all the sas files in folder
  • create a macro variable array of these files
  • make a loop and call editFile macro on each single extracted file.
%let dir=C:\prg;
%let var_prefix=vars_;

%macro batchEdit;
data _null_;
  pfad="&dir.";
  rc=filename("fileref",pfad);
  did=dopen("fileref");
  if did eq 0 then do;
    putlog "Dir does not exist";
    return;
  end;
  num=dnum(did);
  j=1;
  do i=1 to dnum(did);
    name=dread(did,i);
    if UPCASE(substrn(name,max(1,length(name)-3),4)) eq ".SAS" then do;
      call symput(CATS("&var_prefix.",j),name);
      j=j+1;
    end;
  end;
  call symput("varcnt",j);
  rc=dclose(did);
  rc=filename("fileref");
run;

  %DO j=1 %TO &varcnt.;
    %editFile(file=&&&var_prefix&j);
  %END;
%mend;

%macro editFile(file=);
%put In this Macro you should define what modification should be done on each single file;
%put Current File: &file.;
%mend;

%batchEdit;

P.S. I have tested it on unix SAS server, so it might require some modifications on PC SAS.

Upvotes: 0

Related Questions