LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (June 1996, week 3)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Sat, 15 Jun 1996 15:39:02 -0700
Reply-To:     gaylen fraley <gfraley@PRIMENET.COM>
Sender:       "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From:         gaylen fraley <gfraley@PRIMENET.COM>
Organization: Primenet Services for the Internet
Subject:      Re: Retain statement

> > gaylen fraley wrote: > > > > > > > > > > Raymond, > > > > > > No, It makes no difference where you put the RETAIN statement. It is > > > non-procedural, retaining values of any specified variables, whatever > > > position it is placed in. > > > > > > Retain variables may include ones that are read from a SET (or MERGEed) > > > dataset - these variables will only be overwritten on execution of the > > > SET (or MERGE) statement, so the position of this _is_ important, > > > particularly when using multiple SETs (or MERGEs) . > > > > > > Hope this is helpful, > > > > > > Bruce > > > > > > MRGRXL@NCAL.KAIPERM.ORG wrote: > > > > > > > > Dear SAS-gurus, > > > > > > > > I always use RETAIN statement before SET statement. When I started to use > > > > RETAIN statement (10+ years ago), I'm sure that I had a reason why I put > > > > RETAIN statement before SET statement, but I forgot. Does it matter before or > > > > after the SET statement? If it does, could anyone give me specific example? > > > > > > > > I'm not talking about the case that the retain variable happens to be in SET > > > > data. I'm only interested in the case that the retain variable is created for > > > > the first time from the data. > > > > > > > > Thanks! > > > > > > > > Raymond Lee > > > > > > > > P.S. How about the order of RETAIN and LENGTH statements? > > > > > > > This is NOT correct. The placement of the RETAIN statement IS important. The > > following example illustrates this: > > > > data ONE; > > do i = 1 to 10; > > output; > > end; > > run; > > > > data TWO; > > retain _all_; > > set ONE; > > if I = 1 then X = 1; > > run; > > > > /* According to your statement, all 10 records should retain the value of X=1. */ > > /* You will discover that X=1 is only in the first record. If, however, you */ > > /* move the RETAIN staement AFTER the IF i=1 statement, THEN all 10 records will */ > > /* retain the value for X. */ > > > > Gaylen Fraley \\||// > > Team OS/2 ( .. ) > > -----------------------------------oOO--()--OOo-------------------------------- > > John 3:16 "For God so loved the world, that He gave His only begotten Son, > > that whosoever believeth in Him, should not perish, but have everlasting life." > > ------------------------------------------------------------------------------- > > Sorry, Gaylen. Check your SAS manuals. You'll see that the retain changes everytime > the variable that is retain-ed changes, so if you place it before the set statement, > it will have no value until the first observation of the set is read. Until the > second observation is read in, the variable will retain its value until then, meaning > that, if there is any code between the retain and the set statement refering to the > retain-ed variable, it will perform instruction based on the variable in it's current > state. If that's confusing, I'll show you: > > data ONE; /* contains 10 obs and 1 variable */ > do i = 1 to 10; > output; > end; > run; > > data TWO; > retain _all_; /* meaning var 'I' */ > if I = 2 then x = 10; else x = I; > set ONE; > if I = 1 then X = 1; > run; > > In this case, the first time this is read, I has no value, therefore x will have no > value, but it will have a value once the first observation is read and the next > statement after the set is executed. The first observation to print will have the > following values: I = 1, x = 1. > > On the second run-thru, I will still = 1, so x will = 1. Since the next condition > test is looking for I = 1 (and I will be 2), x will still be 1. The third iteration > will yield, once the output is done, I = 3, x = 10, because I was still 2 when it > went thru the next time. Try it. > > I got to learn all about this stuff the hard way. Trust me, it's not fun, but you > learn sooooooo much! > > Pat Flickner > pflick@op.net >

No Pat, you are in error.

X is not being retained in your example. You are setting it each time. Created variables, not those in the SET dataset, are not in the vector at the time of the retain are not RETAINED. Your ELSE statement always forces a value to X. That is why X always equals I after the third iteration. If it was actually RETAINED, it would have a value of !0 after the third iteration, without the ELSE statement. Try it. Take the ELSE statement out. If you are correct, then X should always have a retained value of 10, after the third iteration. It won't, unless you move the RETAIN statement after X is assigned at least once, or explicitly retained as in RETAIN X.

Pat, I don't claim to be an expert. I am still learning. I have been developing with SAS since 1979 and I, too, have found out things the hard way. In this case, I know I am right. Your ELSE coding is fogging your understanding of the logic of the RETAIN statement.

Gaylen Fraley \\||// Team OS/2 ( .. ) -----------------------------------oOO--()--OOo-------------------------------- John 3:16 "For God so loved the world, that He gave His only begotten Son, that whosoever believeth in Him, should not perish, but have everlasting life." -------------------------------------------------------------------------------


Back to: Top of message | Previous page | Main SAS-L page