Newsgroups: bit.listserv.sas-l
Date:         Wed, 11 Nov 1992 16:07:03 PST
Reply-To:     Thomas M Skinner <Thomas_M_Skinner@CCM.HF.INTEL.COM>
From:         Thomas M Skinner <Thomas_M_Skinner@CCM.HF.INTEL.COM>
Subject:      Re[3]: VIEWs & RENAMEs
Comments: To: Bert_Cave@ccm.hf.intel.com, SAS-L@uga.cc.uga.edu

Dear SAS-Lers:

Addendum to my last post:


>Rich Levin replies to Howard Schreier's mailing:


     >>Page 173 of the SAS Language manual (Version 6 Reference) shows
     >>the order of precedence of DROP, KEEP, and RENAME statements and
     >>options. This documentation suggests that your code should work
     >>as this precedence changes. I tested the same code above, but changed
     >>"keep = newname" to "keep = oldname", and it worked fine.

     >>I would guess that this change in precedence occurs because the
     >>view only stores the compiled data step. Perhaps part of the
     >>documented precedence relationship occurs at execution time?
     >>Anyway, if you use KEEP and RENAME the same way, (either both KEEP=
     >>and RENAME= or both KEEP and RENAME) then the precedence will
     >>always occur as documented.

>Reply:

>Richard, You're right to point out the order of precedence.  In fact, the
>RENAME "STATEMENT" enjoys a dual role:  The old name is used in other program
>statements, and the new name is used in the output data set options, the
>question that remains is whether the former property (old name used in program
>statements) is in effect at compile rather than execution, and additionally
>whether the KEEP statement or data statement option (KEEP=) has a role in all
>this.  To test this I submitted the following:
>
>
 >14   data test;
 >15        old = 1 ;
 >
 >
 >NOTE: The data set WORK.TEST has 1 observations and 1 variables.
 >NOTE: The DATA statement used 0.01 CPU seconds and 5062K.
 >
 >
 >16   data test4 / view = test4;
 >17        set test;
 >18   keep new;
 >19   rename old = new;


 >NOTE: DATA STEP view saved on file WORK.TEST4.
 >NOTE: The original source statements cannot be retrieved from a stored DATA
       >STEP view nor will a stored DATA STEP view run under a different
       >release of the SAS system or under a different operating system.
       >Please be sure to save the source statements for this DATA STEP view.
 >NOTE: The DATA statement used 0.01 CPU seconds and 5062K.

 >20   proc print data = test4; var new; run;

 >ERROR: Variable NEW not found.  Note: in fact there are 0 varaibles in the
 >set.


 >NOTE: The view WORK.TEST4.VIEW used 0.04 CPU seconds and 5062K.
 >NOTE: The SAS System stopped processing this step because of errors.
 >NOTE: The PROCEDURE PRINT used 0.00 CPU seconds and 5062K.


>Note: the order of the KEEP and RENAME statements has no effect here. What's
>interesting here, is the KEEP STATEMENT did not generate a error or warning.
>The net result was the output dataset has no variables at all, indicating again
>that the RENAME STATEMENT did not take effect, while the KEEP did (even though
>the result of which was no variables kept).  A similar test eliminating the
>KEEP statement resulted in a dataset with the new name, indicating that the
>RENAME STATEMENT indeed has an effect in the VIEW, however it is confounded, or
>somehow disabled by the KEEP, be it statement or option.

>And if the audience out there isn't confounded yet, I'd be amazed...

>So the moral of the story is:  If using a KEEP statement or KEEP= option in a
>view with a corresponding newly named variable via RENAME, be sure to use the
>RENAME= OPTION on the SET STATEMENT..and not the RENAME STATEMENT.  This
>solution requires using the NEW name in all program statements. Obviously,
>there is a loss of flexibility and functionality associated with this,
>Soooooo..Perhaps SI can take a look at it.
>'NUF SAID.

So I Thought..til I thought about the use of the RENAME= option on the output
data statement....such as DATA TEST (RENAME = (old = new)..

Here are some examples:


 19   data test1(
 20              rename = (old = new)
 21              keep   = new
 22             )
 23             / view = test1;
 24   old = 1;

 NOTE: DATA STEP view saved on file WORK.TEST1.
 NOTE: The original source statements cannot be retrieved from a stored DATA
       STEP view nor will a stored DATA STEP view run under a different
       release of the SAS system or under a different operating system.
       Please be sure to save the source statements for this DATA STEP view.
 NOTE: The DATA statement used 0.01 CPU seconds and 4914K.

 25   proc print data = test1;
 26        var new;
 ERROR: Variable NEW not found.
 27   run;
 NOTE: The view WORK.TEST1.VIEW used 0.03 CPU seconds and 4914K.
 NOTE: The SAS System stopped processing this step because of errors.
 NOTE: The PROCEDURE PRINT used 0.00 CPU seconds and 4914K.


 28   data test2(
 29              keep   = new           <---- changed the order here
 30              rename = (old = new)
 31             )
 32             / view = test2;
 33   old = 1;

 NOTE: DATA STEP view saved on file WORK.TEST2.
 NOTE: The original source statements cannot be retrieved from a stored DATA
       STEP view nor will a stored DATA STEP view run under a different
       release of the SAS system or under a different operating system.
       Please be sure to save the source statements for this DATA STEP view.
 NOTE: The DATA statement used 0.00 CPU seconds and 4914K.


 34   proc print data = test2;
 35        var new;
 ERROR: Variable NEW not found.
 36   run;

 NOTE: The view WORK.TEST2.VIEW used 0.03 CPU seconds and 4914K.
 NOTE: The SAS System stopped processing this step because of errors.
 NOTE: The PROCEDURE PRINT used 0.00 CPU seconds and 4914K.


With a KEEP= option, and the RENAME= option on the output data set statement, it
malfunctions just as it does with a RENAME STATEMENT, REGARLESS OF THE ORDER OF
THE OPTIONS.


 37   data test3(
 38              rename = (old = new)
 39             )
 40             / view = test3;
 41   old = 1;

 NOTE: DATA STEP view saved on file WORK.TEST3.
 NOTE: The original source statements cannot be retrieved from a stored DATA
       STEP view nor will a stored DATA STEP view run under a different
       release of the SAS system or under a different operating system.
       Please be sure to save the source statements for this DATA STEP view.
 NOTE: The DATA statement used 0.00 CPU seconds and 4976K.

 42   proc print data = test3;
 43        var new;
 44   run;

 NOTE: The view WORK.TEST3.VIEW used 0.04 CPU seconds and 5042K.
 NOTE: The PROCEDURE PRINT used 0.01 CPU seconds and 5042K.


Again, by itself it works fine..


So it would appears that when creating a data step VIEW the only way to rename
variables and keep the newly named variables with a keep statement is to rename
in the SET STATEMENT with the RENAME= option.  This can't be the way SI wants
it, can it?  Or is just an undocumented "feature".


Intel-Insider,

Tom Skinner

================================================================================
T.M. Skinner                      Internet: Thomas_M_Skinner@ccm.hf.intel.com
Sr. SAS Technical Support             Mail: 1900 Prairie City Rd. MS FM1-102
Intel Corporation                           Folsom, CA 95630
================================================================================