LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous (more recent) messageNext (less recent) messagePrevious (more recent) in topicNext (less recent) in topicPrevious (more recent) by same authorNext (less recent) by same authorPrevious page (April 2004, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 23 Apr 2004 08:18:05 -0400
Reply-To:     Ed Heaton <EdHeaton@WESTAT.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Ed Heaton <EdHeaton@WESTAT.COM>
Subject:      Re: Searching backward through a file.
Comments: To: "don_stanley@paradise.net.nz" <don_stanley@paradise.net.nz>
Content-Type: text/plain

We have an in-house application that writes the SAS code. It uses some user input, mostly logical expressions. Some of the code that writes the program is WinBatch, but mostly it's SAS code that writes SAS code. I did not design the system, but I try to help the users when they have problems.

Since the code-writing program is not as smart as a human programmer, the code it produces is not terribly efficient and it is extremely wordy. But it does a lot. It reads hierarchical, positional ASCII files and reports on range errors and logic errors. It also reads ASCII update files and updates the hierarchical data files.

I hope you can appreciate why I don't want to wade through so many lines of code. The situation that prompted me to develop this tool was a very hard-to-find problem that turned out to be a single-quote at the end of a line of the user-defined logic file -- probably caused by bumping the quote key while aiming for the enter key. Because of SAS's 32k character-string limit, the errors did not start until much later in the program. So, it wasn't due to mismatched parens at all.

Here is my code as it stands today.

FileName sasCode "ChildAssessmentChk.sas" ; %let lRecL = 120 ; Data sasCode( keep= n line ) ; Length n 5 line $&lRecL ; InFile sasCode lRecL=&lRecL ; Input ; n = _n_ ; line = _inFile_ ; If not missing(line) then do i=1 to length(line) ; Select ( rank( subStr( line , i , 1 ) ) ) ; When (40) leftParen + 1 ; When (41) rightParen + 1 ; Otherwise ; End ; End ; If ( leftParen lt rightParen ) then do ; Put "ERROR: Extra right parenthesis on line " n "." ; Do until ( leftParen eq rightParen ) ; rightParen + -1 ; End ; End ; Run ; Proc sort data=sasCode ; By descending n ; Run ; Data _null_ ; Set sasCode ; If not missing(line) then do i=1 to length(line) ; Select ( rank( subStr( line , i , 1 ) ) ) ; When (40) leftParen + 1 ; When (41) rightParen + 1 ; Otherwise ; End ; End ; If ( leftParen gt rightParen ) then do ; Put "ERROR: Extra left parenthesis on line " n "." ; Do until ( leftParen eq rightParen ) ; leftParen + -1 ; End ; End ; Run ;

Ed

Edward Heaton, SAS Senior Systems Analyst, Westat (An Employee-Owned Research Corporation), 1600 Research Boulevard, RW-3541, Rockville, MD 20850-3195 Voice: (301) 610-4818 Fax: (301) 610-5128 mailto:EdHeaton@Westat.com http://www.Westat.com

-----Original Message----- From: Don & Susann [mailto:don_stanley@PARADISE.NET.NZ] Sent: Thursday, April 22, 2004 4:07 PM To: SAS-L@LISTSERV.UGA.EDU Subject: Re: Searching backward through a file.

But you didn't write 44,000 lines of code did you? What was used to generate the code .. is it easier to examine the code generators to look for this? Is the whole program a single data step (assuming your unbal parenthesis are in a data step?) or a series of data steps?

If the pseudo code your presented were possible, you would not need to search from the bottom, only from where the unbalanced parenthesis error was reported.

Hard one to sort out! I know how difficult it can to sort out code created by code generators, assuming thats what you do. My technique whenever possible is to comment out pieces of datastep, and gradually add more and more to the commented out code until the error no longer occurs. Then you have a good idea of where to examine the code in detail. But be wary, this can induce other errors.

Good luck! Don

Ed Heaton wrote: > Richard, > > The code that I often have to debug can have statements that span dozens of > lines and the files are tens of thousands of lines long. (The current one > is 44 thousand lines of code! > > Ed > > -----Original Message----- > From: Richard A. DeVenezia [mailto:radevenz@ix.netcom.com] > Sent: Thursday, April 22, 2004 12:05 PM > To: Ed Heaton > Subject: Re: Searching backward through a file. > > > Ed Heaton wrote: > >>Good morning, all; >> >>I am trying to create a little job to search SAS code for unbalanced >>parentheses. The following will alert me if I have an extra right >>parenthesis. >> >>Data _null_ ; >> InFile sasCode end=eof ; >> Input ; >> If not missing(_inFile_) then do i=1 to length(_inFile_) ; >> Select ( rank( subStr( _inFile_ , i , 1 ) ) ) ; >> When (40) leftParen + 1 ; >> When (41) rightParen + 1 ; >> Otherwise ; >> End ; >> If ( leftParen lt rightParen ) then put _all_ ; >> End ; >> If eof then call symPut( "n" , put( _n_ , best. ) ) ; >>Run ; >> >>Now I want to search for an extra left parenthesis. I envision >>starting at the bottom of the file and searching backward. Something >>like... >> >>Data _null_ ; >> Retain N &n ; >> InFile sasCode ; >> /* Go to the bottom of the file. */ >> Input #N ; >> N + -1 ; >> If not missing(_inFile_) then do i=length(_inFile_) to 1 by -1 ; >> Select ( rank( subStr( _inFile_ , i , 1 ) ) ) ; >> When (40) leftParen + 1 ; >> When (41) rightParen + 1 ; >> Otherwise ; >> End ; >> If ( leftParen gt rightParen ) then put _all_ ; >> End ; >> If ( _n_ eq &n ) then stop ; >>Run ; >> >>Of course, this doesn't work. Is there a way to make SAS read >>backward through a file. My other option is to write each line from >>the first read into a data set, reverse the order of the data set, >>and use it in the second DATA step. This seems very clunky to me. >>Any suggestions? >> >>Ed >> >>Edward Heaton, SAS Senior Systems Analyst, >>Westat (An Employee-Owned Research Corporation), >>1600 Research Boulevard, RW-3541, Rockville, MD 20850-3195 >>Voice: (301) 610-4818 Fax: (301) 610-5128 >>mailto:EdHeaton@Westat.com http://www.Westat.com > > > Ed: > > unbalanced is number of opens NE number of closes ;) > > Of course this code would never work for me. My large parenthetical > expressions are usually spread across several lines. Then there is the > issue of comments and escaped context in macro, e.g. %( > > > Richard A. DeVenezia > >

-- Don Stanley, B.SC, Dip O.R.S, MNZCS Author:: Beyond the obvious with SAS Screen Control Language. Author:: Solutions for your GUI Applications Development Using SAS/AF FRAME Technology http://www.syswaregroup.com , http://homepages.rootsweb.com/~ashluke


Back to: Top of message | Previous page | Main SAS-L page