Date: Mon, 9 Dec 2002 23:43:57 -0000
Reply-To: Roland <roland@RASHLEIGH-BERRY.FSNET.CO.UK>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Roland <roland@RASHLEIGH-BERRY.FSNET.CO.UK>
Subject: Re: new "lookahed" macro
"My problem"? Maybe you should RTFM first.
"Ian Whitlock" <WHITLOI1@WESTAT.COM> wrote in message
news:08B08C9FA5EBD311A2CC009027D5BF8102E2B57E@remailnt2-re01.westat.com...
> Rolland,
>
> The first time I ran with some code I had at hand.
>
> 792 data w ;
> 793 input (nbhd1 - nbhd9) ($) ; cards ;
>
> NOTE: The data set WORK.W has 2 observations and 9 variables.
> NOTE: DATA statement used:
> real time 0.00 seconds
>
>
> 796 ;
> 797
> 798 options mprint ;
> 799
> 800 %lookahed(dsin=w,dsout=w2,bygroup=,vars=nbhd1 nbhd2,lookahed=1)
> ERROR: (lookahed) Nothing specified to bygroup= parameter
> ERROR: (lookahed) Leaving lookahed macro due to error(s) listed.
>
> Ok, your error message is at least clear. But it would be nice to handle
> this instead of asking the consumer to solve your problems.
>
> The second time I got no error messages, just a corrupted file.
>
> 801
> 802 data w ;
> 803 input (nbhd1 nbhd11) ($) ; x = 1 ; cards ;
>
> NOTE: The data set WORK.W has 2 observations and 3 variables.
> NOTE: DATA statement used:
> real time 0.00 seconds
>
>
> 806 ;
> 807
> 808 options mprint ;
> 809 %lookahed(dsin=w,dsout=w2,bygroup=x,vars=nbhd1 nbhd11,lookahed=1)
> MPRINT(LOOKAHED): data _look;
> MPRINT(LOOKAHED): retain _seq 0;
> MPRINT(LOOKAHED): set w;
> MPRINT(LOOKAHED): by x;
> MPRINT(LOOKAHED): if first.x then _seq=0;
> MPRINT(LOOKAHED): _seq=_seq+1;
> MPRINT(LOOKAHED): run;
>
> NOTE: There were 2 observations read from the data set WORK.W.
> NOTE: The data set WORK._LOOK has 2 observations and 4 variables.
> NOTE: DATA statement used:
> real time 0.00 seconds
>
>
> MPRINT(LOOKAHED): data _look1;
> MPRINT(LOOKAHED): set _look(keep=_seq x nbhd1 nbhd11);
> MPRINT(LOOKAHED): _seq=_seq-1;
> MPRINT(LOOKAHED): rename
> MPRINT(LOOKAHED): nbhd1=nbhd11 nbhd11=nbhd111 ;
> MPRINT(LOOKAHED): run;
>
> NOTE: There were 2 observations read from the data set WORK._LOOK.
> NOTE: The data set WORK._LOOK1 has 2 observations and 4 variables.
> NOTE: DATA statement used:
> real time 0.05 seconds
>
>
> MPRINT(LOOKAHED): data w2;
> MPRINT(LOOKAHED): merge _look(in=_look) _look1 ;
> MPRINT(LOOKAHED): by x _seq;
> MPRINT(LOOKAHED): if _look;
> MPRINT(LOOKAHED): drop _seq;
> MPRINT(LOOKAHED): run;
>
> NOTE: There were 2 observations read from the data set WORK._LOOK.
> NOTE: There were 2 observations read from the data set WORK._LOOK1.
> NOTE: The data set WORK.W2 has 2 observations and 4 variables.
> NOTE: DATA statement used:
> real time 0.00 seconds
>
>
> MPRINT(LOOKAHED): proc datasets nolist;
> MPRINT(LOOKAHED): delete _look _look1 ;
> MPRINT(LOOKAHED): run;
>
> NOTE: Deleting WORK._LOOK (memtype=DATA).
> NOTE: Deleting WORK._LOOK1 (memtype=DATA).
> MPRINT(LOOKAHED): quit;
>
> NOTE: PROCEDURE DATASETS used:
> real time 0.00 seconds
>
>
> Now perhaps you have the usual programmer's disclaimer that any code is to
> be used at the consumer's risk, since no claim is made to present working
> code. In any case I do think that in the case of public presentation of
> macros advertised for general use, one should at least try to anticipate
> problems and eliminate them with work or at least documentation.
>
> Unfortunately, it is not easy to write good general utility macros in SAS
> because the medium was never intended for that purpose. When I don't get
a
> lot of error complaints from utility code, I figure it is because no one
is
> using the code. It takes an army of users to make very robust code, or at
> least a good test team.
>
> I didn't understand the line
>
> %if not %length(&dsout) %then %let dsout=%scan(&dsin,1,%str(%());
>
> Since it isn't legal to have a left parenthesis in a SAS data set name,
why
> not use the more obvious
>
> %if not %length(&dsout) %then %let dsout=&dsin;
>
> Now coming back to the rename problem, why didn't SAS issue an error
> message? In fact it didn't in version 6.12 either, so my be my surprise
is
> unfounded that it should have was unfounded. To simplify,
>
> data w ( rename = ( x = y y = x ) ) ;
> x = 1 ; y = 2 ;
> run ;
>
> is legal code. And so it is with the RENAME statement also. So here is
the
> problem that I remember.
>
> 1266
> 1267 data w ( rename = ( x = y y = x ) ) ;
> 1268 x = 1 ; y = 2 ;
> 1269 rename x = y y = x ;
> 1270 run ;
>
> WARNING: Variable y cannot be renamed to x because x already exists.
> WARNING: Variable y cannot be renamed to x because x already exists.
> WARNING: The variable x in the DROP, KEEP, or RENAME list has never been
> referenced.
> NOTE: The data set WORK.W has 1 observations and 2 variables.
> NOTE: DATA statement used:
> real time 0.00 seconds
>
> Can anyone hazard a guess as to what is happening here? Is rename
checking
> only done when there was a previous renaming in the same DATA step? Note
> that the warning is written twice so apparently the error is caught in
both
> renamings only when there are two of them (more accurately one RENAME
> statement and RENAME option on the output data set).
>
> IanWhitlock@westat.com
>
> -----Original Message-----
> From: Roland [mailto:roland@RASHLEIGH-BERRY.FSNET.CO.UK]
> Sent: Monday, December 09, 2002 11:54 AM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Re: new "lookahed" macro
>
>
> More precisely located at
> http://mysite.freeserve.com/rolandrb/sasmacros/lookahed.sas
>
> "Roland" <roland@rashleigh-berry.fsnet.co.uk> wrote in message
> news:at2flf$hj9$1@newsg4.svr.pol.co.uk...
> > I've written a macro that will allow you to reference values in
> > observations ahead of the one you are in, hence "lookahed", short for
> "look
> > ahead".
> >
> > http://mysite.freeserve.com/rolandrb/sasmacros/index.html
> >
> >
|