Date: Mon, 24 Jun 2002 16:54:18 +0100
Reply-To: Simon.Gillow@BBG.CO.UK
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Simon Gillow <Simon.Gillow@BBG.CO.UK>
Subject: Caution when using the RETAIN statement??!
Content-type: text/plain; charset="us-ascii"
Hello all,
We often use the RETAIN statement to change the ordering of variables in a
SAS dataset.
It was found that when using a RETAIN statement that if you are using an IF
condition to create/alter the contents of a variable that you need to code
specifically for every eventuality. This is best explained with an example.
The dataset SASHELP.COMPANY contains a variable called level2 which has 3
possible values, TOKYO, LONDON or NEW YORK. This dataset is used in the
following examples.
When I run the following data step:
data company_error;
retain level1 level2 level3 check;
set sashelp.company (keep=level1 level2 level3);
if level2='TOKYO' then check='Tokyo';
else if level2='LONDON' then check='London';
run;
With this coding you would expect check to either contain 'Tokyo', 'London'
or be blank,but that isn't the case!
You will notice that the population of the check variable is correct when
level2 is either TOKYO or LONDON but it still shows 'Tokyo' for NEW YORK,
this is because the RETAIN statement has retained the previous value for
check because NEW YORK wasnt specifically coded for.
Now when I run:
data company_no_error;
retain level1 level2 level3 check;
set sashelp.company (keep=level1 level2 level3);
if level2='TOKYO' then check='Tokyo';
else if level2='LONDON' then check='London';
else check=' '; /*NOTE This line is needed to mop up any instances that arent covered specifically*/
run;
This time the check variable is populated as we would expect.
Can anyone suggest why this happened? Are there any other pitfalls involved
with the use of RETAIN?
Thanks,
Simon
**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.
This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.
Bradford & Bingley plc
Registered Office: PO Box 88, Croft Road, Crossflatts, Bingley, West Yorkshire, BD16 2UA
Registered in England No. 3938288
Regulated by the Financial Services Authority and a Member of the General Insurance Standards Council.
http://www.bbg.co.uk
Bradford Bingley plc
**********************************************************************