BookmarkSubscribeRSS Feed
Pra
Calcite | Level 5 Pra
Calcite | Level 5

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!!!

8 REPLIES 8
data_null__
Jade | Level 19

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.

Pra
Calcite | Level 5 Pra
Calcite | Level 5

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...

Reeza
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Kurt_Bremser
Super User

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.

art297
Opal | Level 21

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.


FriedEgg
SAS Employee

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

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

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 8 replies
  • 1370 views
  • 3 likes
  • 7 in conversation