"read & edit sasa programs" using sas program

Reply
Contributor
Posts: 65

"read & edit sasa programs" using sas program

Hi,

I am looking for a code that can read sas program, as similar data form external file ignoring the commented portion in the program

.

Also if there is any way that can edit a part of the program read, that would be more helpful.

Is there procedure or function to do that, I need this for automating a process which needs many similar minor updates to be done for most of the programs.

Please help if you have done something similar or you have some idea regarding this.

Thanks and Regards,

Moovendhan Mudhaliar

Super User
Posts: 11,343

Re: "read & edit sasa programs" using sas program

Posted in reply to DMoovendhan

There may be other options that will be easier to maintain and use. Can you provide some examples of the "many similar minor updates" you are likely to encounter? It may be that macros with parameters may be a workable solution.

Super Contributor
Posts: 339

Re: "read & edit sasa programs" using sas program

Posted in reply to DMoovendhan

This reads to me like a regular expression job. Typically you would do this with something like a PERL script or even reading your sas program in C/C++/C# as text stream line by line and use a well built regular expression to search&replace.

It is doable from within SAS but it becomes extremely complex when say, the sas programmer had different habits with /* */ versus *...; comments. or when /* and */ for the same comment are on different lines as then you need to read your code somehow else.

Unless you provide very specific details or at least examples as to what small modifications to the program you would want to do, it is very hard to provide more help than

1. Go get a book about perl regular expression (since sas regular expressions PRX use the perl syntax) - I read Mastering regular expressions : powerful techniques for Perl and other tools / Jeffrey E.F. Friedl. a few months ago and there were definitely very good hints on how to avoid loop issues like avoiding to read comments within double quotes as comments but rather as text in the double quote and whatnot. Something of the like would be very appropriate after you familiarize yourself with regular expressions

2. Write a program to read your code into a SAS dataset. You may need to read it write it to a file, read it again and so on with different delimiter/flowover options to manipulate around comments. Typically the final preprocessed program should have one record per delimited comment and one record per statement (as delimited by semi column). This makes for significantly easier PRX processing.

3. Tests your PRX on copies of your SAS program multiple times over iterations as you "debug" the PRX or rather the lack of human not thinking about this and that and using classes of characters that were too wide for the desired result making the PRX engine match more than what was intended

If you can provide us with a decent .sas code file, preferably one that represents the worst case scenario of programming syntax structure and either a desired final result done manually or a list of what your program needs to do, we can help further.

Vincent

New Contributor
Posts: 4

Re: "read & edit sasa programs" using sas program

Posted in reply to DMoovendhan

Since a SAS program is a text file, you can read it in using INPUT, modify lines of code using IF/THEN logic and functions applied to _INFILE_ and then write out a new version of the program using a FILE statement (preferably with a new name to avoid overwriting the original version).

See article: http://www.nesug.org/proceedings/nesug02/cc/cc014.pdf

Contributor ckx
Contributor
Posts: 59

Re: "read & edit sasa programs" using sas program

Posted in reply to DMoovendhan

Hello Moovendhan,

I've done something similar to what your asking in Javascript macros for the freeware text editor PSPad. I use scripts to read the SAS file into memory, delete comments, and then report macros, datasets, etc.Since this uses regular expressions, it might work in SAS as well using prxreplace. Here's a javascript function for removing comments:

function noComments(instring) {

    // use [\s\S] to match anything, including newlines, for a multiline search

    // use *? for a non-greedy search, i.e. match the first "*/" occurrence

    // First remove all multiline comments /* ... */

    var re=/\/\*[\s\S]*?\*\//ig;

    var nocomm=instring.replace(re,"");

   

    // remove all single line comments

    var re=/(^|\s+)\*[\s\S]*?;/ig;

    var nocomm2=nocomm.replace(re,"");

    return(nocomm2);

}

Good luck, hope it works!

John Hendrickx

Super Contributor
Posts: 339

Re: "read & edit sasa programs" using sas program

Interesting regexes John. The only difference if they had to be used in SAS is that datasets don't have any "newlines" so the difficulty would be to read the multiline comments appropriately into SAS dataset or to adapt the loop to account for the fact that a comment can begin in one record and end a few records later.

The as few as possible quantifiers really help simplify the regex write-up. The only issue with this however is if you have embeded comments, you will leave a */ in the code like

/* if a=b then output; /* Testing a condition */ */ would match /* if a=b then output; /* Testing a condition */ and leave */ in the final code. Modifying the regex to account for that can be somewhat hectic since you don't attempt to match a single character but a 2char string (by further defining your [\s\S]* read-all such that it does not accept /*)

Nonetheless this gives a lot of food for toughts.

Ask a Question
Discussion stats
  • 5 replies
  • 313 views
  • 0 likes
  • 5 in conversation