Date: Thu, 22 Apr 2004 13:26:57 -0400
Reply-To: Venky Chakravarthy <venky.chakravarthy@PFIZER.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Venky Chakravarthy <venky.chakravarthy@PFIZER.COM>
Subject: Re: Searching backward through a file.
On Thu, 22 Apr 2004 09:47:01 -0700, Choate, Paul@DDS <pchoate@DDS.CA.GOV>
wrote:
>This is off your question Ed, but why not count parentheses between ";"
>delimiters and keep the line numbers of the ";" and the prior ";" that
>bracket the unbalanced count?
>
>Paul Choate
>DDS Data Extraction
>(916) 654-2160
>
>-----Original Message-----
>From: Ed Heaton [mailto:EdHeaton@WESTAT.COM]
>Sent: Thursday, April 22, 2004 8:41 AM
>To: SAS-L@LISTSERV.UGA.EDU
>Subject: Searching backward through a file.
>
>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
Hi Paul,
I was thinking along those lines. The one problem that occurred to me was
calls to macros. Suppose we have two consequtive calls to a macro one with
an extra left and another with an extra right paren. Further assume we have
good coding practices by not having a semicolon after a call to a macro.
These errors will cancel each other out before the next ";" is seen by the
parser.
Considering that the variables LeftParen and RightParen are sum
accumulators with an automatic retain and following Ed's logic why not
simply do a test of "if leftparen ^= rightparen then put _all_;" when
testing for EOF. The first test can always be a total number of leftparens
to equal rightparens. Otherwise we make the assumption that every single
line of code must have matching left and right parens. That would be too
strict an assumption to make .. at least for my codes.
Kind Regards,
Venky Chakravarthy
|