Date: Mon, 26 Apr 2004 18:03:07 +0000
Reply-To: iw1junk@COMCAST.NET
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Ian Whitlock <iw1junk@COMCAST.NET>
Subject: RENAME and KEEP in one data step
Michael Whitcomb <mwhitcomb@WESLEYAN.EDU> asks about the change in the kept variable of the two steps.
data two (RENAME = (oldvar = newvar));
set one;
keep OLDVAR;
run;
data two;
set one (rename = (oldvar = newvar ));;
keep NEWVAR;
run;
In the first case the dataset option acts last. So the variable is OLDVAR and that is what must be kept and renamed.
In the second case the dataset option acts first and defines the name in the PDV. So the kept variable must be the renamed version.
I prefer
dataset ( keep = oldvar rename = ( oldvar = newvar ) )
either on the input dataset or the output dataset, because I find the actions of the dataset options clear and those of the statements a bit murky. For options the rule is simple - keep what you have before name changes. Now consider:
54 data w ;
55 oldvar = 1 ;
56 run ;
NOTE: The data set WORK.W has 1 observations and 1 variables.
NOTE: DATA statement used:
real time 0.01 seconds
cpu time 0.01 seconds
57
58 data q ;
59 set w ;
60 rename oldvar = newvar ;
61 keep oldvar ;
62 put _all_ ;
63 run;
oldvar=1 _ERROR_=0 _N_=1
NOTE: There were 1 observations read from the data set WORK.W.
NOTE: The data set WORK.Q has 1 observations and 1 variables.
NOTE: DATA statement used:
real time 0.04 seconds
cpu time 0.03 seconds
Note that both the KEEP and RENAME statements apply to the output file. Hence the variable is OLDVAR in the PDV and the order of the KEEP and RENAME statements in this case is irrelevant, but misleading. For me it is easier to just remember not to use RENAME statements.
The discovery that you can both keep and rename variables in the same step should not come as a surprise since the actions are different and one often wants to do both. Now if you add DROP to the picture just remember that DROP
overrules KEEP.
In summary options on input change the PDV, options on output change the output file, and statements are appiled before output options. However remember that in any case these actions are compile time directives and therefor affect how the code is implemented; they are not actions repeated on every iteration of the DATA step. For more information and examples see
<http://www2.sas.com/proceedings/sugi22/ADVTUTOR/PAPER34.PDF>
--
Ian_Whitlock@comcast.net
begin 666 InterScan_Disclaimer.txt
M5&AI<R!E+6UA:6P@86YD(&%N>2!F:6QE<R!T<F%N<VUI='1E9"!W:71H(&ET
M(&%R92!F;W(@=&AE('-O;&4@=7-E(&]F('1H92!I;G1E;F1E9"!R96-I<&EE
M;G0H<RD@86YD(&UA>2!C;VYT86EN(&-O;F9I9&5N=&EA;"!A;F0@<')I=FEL
M96=E9"!I;F9O<FUA=&EO;BX-"DEF('EO=2!A<F4@;F]T('1H92!I;G1E;F1E
M9"!R96-I<&EE;G0L('!L96%S92!C;VYT86-T('1H92!S96YD97(@8GD@<F5P
M;'D@92UM86EL(&%N9"!D97-T<F]Y(&%L;"!C;W!I97,@;V8@=&AE(&]R:6=I
M;F%L(&UE<W-A9V4N( T*06YY('5N875T:&]R:7-E9"!R979I97<L('5S92P@
M9&ES8VQO<W5R92P@9&ES<V5M:6YA=&EO;BP@9F]R=V%R9&EN9RP@<')I;G1I
M;F<@;W(@8V]P>6EN9R!O9B!T:&ES(&5M86EL(&]R(&%N>2!A8W1I;VX@=&%K
M96X@:6X@<F5L:6%N8V4@;VX@=&AI<R!E+6UA:6P@:7,@<W1R:6-T;'D@#0IP
M<F]H:6)I=&5D(&%N9"!M87D@8F4@=6YL87=F=6PN#0H-"@D)5FES:70@=7,@
=870@:'1T<#HO+W=W=RYC;V=N:7IA;G0N8V]M#0H
end