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!!!
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.
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...
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.
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.
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.
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.
You have a serious problem with WETness. DRY your code.
Since you have to go through all 300+ files anyway, this is the time to extract all the common code pieces into includes.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.