Date: Tue, 16 Mar 2010 18:32:43 -0400
Reply-To: "Kirby, Ted" <ted.kirby@LEWIN.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Kirby, Ted" <ted.kirby@LEWIN.COM>
Subject: Re: how to retain scores
In-Reply-To: A<990a7aef-d1a9-4413-ac6e-0bd1edfa36d0@p3g2000pra.googlegroups.com>
Content-Type: text/plain; charset="us-ascii"
Try this. It is a bit verbose, but seems to work on your test data:
data have;
input id score;
datalines;
1 2.8
1 .
1 3.5
1 .
1 4.0
2 2.2
2 .
2 .
2 2.3
2 .
4 5.5
4 5.7
4 .
4 .
4 4.6
4 .
;
run;
proc print; run;
data want(drop=tscore oldscore);
set have;
retain oldscore;
if first.id then oldscore=score;
/* Need to account for the possibility of consecutive missing values,
so
will introduce a new variable tscore as a temporary variable. */
else do;
tscore=lag(score);
if tscore ne . then oldscore=tscore;
end;
if score=. then score=oldscore;
run;
proc print; run;
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of sas
analysis
Sent: Tuesday, March 16, 2010 5:10 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: how to retain scores
Hi All,
I have scores for each subject at various time points. In certain
visits, scores were missing but in others recorded.
How can I input scores where missing based on the previous score. and
when a new score is recorded it remains and the following that are
missing take its value. This is a large dataset with more than 10000
observations. I have tried retain with first.subject code.
This is how the data looks:
Subject Score
1 2.8
1 .
1 3.5
1 .
1 4.0
2 2.2
2 .
2 .
2 2.3
2 .
4 5.5
4 5.7
4 .
4 .
4 4.6
4 .
This is how I want the data to look:
Subject Score
1 2.8
1 2.8
1 3.5
1 3.5
1 4.0
2 2.2
2 2.2
2 2.2
2 2.3
2 2.3
4 5.5
4 5.7
4 5.7
4 5.7
4 4.6
4 4.6
This is what I am trying to code:
data x;
set y;
retain scores;
if first.scores then score2;
else score2=scores;
run;
any ideas would be great?
Thanks!
************* IMPORTANT - PLEASE READ ********************
This e-mail, including attachments, may include confidential and/or proprietary information, and may be used only by the person or entity to which it is addressed. If the reader of this e-mail is not the intended recipient or his or her authorized agent, the reader is hereby notified that any dissemination, distribution or copying of this e-mail is prohibited. If you have received this e-mail in error, please notify the sender by replying to this message and delete this e-mail immediately.