DATA Step, Macro, Functions and more

Replace Code in .sas File

Reply
Occasional Contributor Pra
Occasional Contributor
Posts: 6

Replace Code in .sas File

Dear All,

Thank you very much in helping and clarifying doubts of my previous questions,

Here i have 300+ sas codes(.sas files) where i have to replace block of code in all of them.

Piece of code :


%if &envi eq "UAT" %then %do;
Proc export data=&abc.

outfile ="&output\&abc.xls" replace;

run;

%end;

%else %if &envi eq "PRD" %then %do;

data out.&abc;

set &abc;

run;

%end;

Part of code (in Bold) to be replaced with  :

Proc export data=&abc.

outfile ="&output\&abc.xls" replace;

run;

** &abc is the last data set created just before running the final export statement.

Thanks in advance!!!

Respected Advisor
Posts: 3,799

Re: Replace Code in .sas File

The code you show is contained within a macro, my question is why is that macro repeated in all 300+ files.

I would think you should remove the entire macro from %MACRO to %MEND and just leave the call.  Then you will have to modify only one file the macro.

You will have to modify the 300+ files to remove the macro definition but that will be easier and make you look like you know what you're doing.

Occasional Contributor Pra
Occasional Contributor
Posts: 6

Re: Replace Code in .sas File

Posted in reply to data_null__

300+ each are separate codes for each reports and each of the code has this block of codes, which needs to be replaced. here macro condition will decided based on the environment it is run in and execute the steps accordingly...

Super User
Posts: 19,770

Re: Replace Code in .sas File

Look into scripting programs to replace text instead of SAS. Assuming you're on linux and replacing the data step with proc export because of that:

Unix Sed Tutorial: Find and Replace Text Inside a File Using RegEx

Otherwise if you're on Windows I'd assign a libname out to an Excel file instead and use that method to export. You can insert that into the top of the file fairly easily, and close the reference at the end of the file...possibly. Not pretty but it would be quick.

Super User
Super User
Posts: 7,942

Re: Replace Code in .sas File

I totally agree with FriedEgg and KurtBremser here.  It would seem that you have multiple repetition (300 files doing same thing, why?), possibly a bad setup structure, and outdated technology (XLS is a previous MS binary format which is not portable, or open) going on there just from the snippet and text given.  I would suggest that you have a good look at what you want to be done, sit down with the relevant consumers of this tool, work out a structured plan (Functional Design Spec + Testing log), modularize the code.  I.e. treat this as a migration: http://en.wikipedia.org/wiki/Software_modernization

As for just having a tool run through and change code, I would suggest that it quite a dangerous approach, do you have a plan in place to document all the changes, each one would need to be thoroughly QC'd etc.

Super User
Posts: 7,761

Re: Replace Code in .sas File

If you already had extracted that code to a single include, you would have to change only one file, done in minutes (2 hours including unittest/documenting/checkin-checkout etc)

Think like a programmer: avoid manual repetitive work at all costs, because that's what you have the computer for!

Being lazy is a good trait in a programmer/developer.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
PROC Star
Posts: 7,467

Re: Replace Code in .sas File

If I were you I would test the code before making the changes. Specifically,

outfile ="&output\&abc.xls" replace;


probably should be:

outfile ="&output\&abc..xls" replace;


Regardless, you can always create a file with one variable per record that indicates the names of all of the files you want to change (or, if they're all in the same directory, use the directory command with a pipe to capture the names of all of the .sas files).


Then, you could write a program that inputs each line of those files as text, find and skip the lines you want to replace, write out all of the other lines and, when you get to the lines you want to replace, write out the replacement lines.


But I agree with data_null_. Rather than just replace the lines, I'd replace them with a macro that contains your revised code. That way, if you have to change it again, you just have to change one macro.


Trusted Advisor
Posts: 1,301

Re: Replace Code in .sas File

You have a serious problem with WETness.  DRY your code.

http://en.wikipedia.org/wiki/Don%27t_repeat_yourself

Super User
Posts: 7,761

Re: Replace Code in .sas File

Since you have to go through all 300+ files anyway, this is the time to extract all the common code pieces into includes.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 8 replies
  • 404 views
  • 3 likes
  • 7 in conversation