Date: Thu, 6 Jun 1996 20:42:00 -0700
Reply-To: MRGRXL@NCAL.KAIPERM.ORG
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: "Raymond K. Lee" <MRGRXL@NCAL.KAIPERM.ORG>
Subject: Retain statement (summary)
As usual, I received many reponses from SAS-gurus. Most of them claimed that
there is no effect on data value with the reverse order of SET and RETAIN
statement because they are not "executable" statements. Some of them mentioned
that the position of the variables will matter even though it's not usually
issue with SAS. Since no one gave me a specific example, I decided to try on a
several examples myself and I'd like to share the result with you.
This is what I found so far:
1. As long as you don't use the same variable name for a RETAIN statement that
it exists in SET statement data, the order of two statements will give you
very little effect (order of variables). However, you will be suprised how
data value changes if you use the same name. I'll give you my example at the
end of this mail, and welcome to try yourself. I guess main issue is
"Attribte" setup.
2. I also asked about the order of RETAIN and LENGTH statemnets. Even though
both are "non-executable" statemnets, the order will make big difference
because of ATTRIBUTE issue. Especailly, a character variable case is crucial.
Also, if the variable exists in the SET statement data, it will give you
more problem.
These are my examples:
* test data *;
data old;
do x = 1 to 50 by 7;
y = put(x,z4.);
output;
end;
run;
* the same variable is used, and SET statement before RETAIN and LENGTH;
data new1;
set old;
retain y '0';
length y $3 ; * length still be $4 ;
if x > 20 then y ='123456';
run;
* the same variable is used, and SET statement after RETAIN and LENGTH ;
data new2;
retain y '0';
length y $3 ; * length will be $1 because of retain;
set old;
if x > 20 then y ='123456';
run;
data new3;
length y $3 ; * Now, length will be $3, but will be trunc.;
retain y '0';
set old;
if x > 20 then y ='123456';
run;
If you give not enough length value, see what happen to the result...
* test data;
data old;
do x = 1 to 50 by 7;
y = x + 123456 ; * length is 8 at this moment;
output;
end;
run;
data new1;
retain y 0 ;
length y 3 ; * length 3 is not enough for y value;
set old;
if x > 20 then y = 999 ;
run;
Check your result! You will be suprised... No warning or error message...
Raymond Lee