DATA Step, Macro, Functions and more

Last observation carrying forward for a subject.

Reply
Frequent Contributor
Posts: 81

Last observation carrying forward for a subject.


data gd;
input id$ visit sbp dbp;
cards;
001 10 120 90
001 10 110 .
001 10 130 .
001 20 110 90
001 30 110 98
002 10 120 .
002 10 110 80
002 10 130 98
002 20 110 90
002 30 110 98
003 10 120 90
003 10 110 80
003 10 130 98
003 10 110 90
003 20 120 .
003 30 110 98
;
run;

 

For a subject if sys, dbp is missing after first record we need to return first value to it missing value to it next records. If first record is also missing for a subject we need to set to missing.

 

My Code:

 

proc sort data=gd;by id visit;run;

 

data gd4;
set gd;
by id visit;
if first.id;
b=dbp;
run;


data gd6;
merge gd gd4;
by id;
/*if missing(dbp) then dbp=b;*/
/*else b=dbp;*/
/*b=dbp;*/
run;

 

Super User
Posts: 22,850

Re: Last observation carrying forward for a subject.

Posted in reply to rajeshalwayswel

Look up RETAIN

Trusted Advisor
Posts: 1,288

Re: Last observation carrying forward for a subject.

Posted in reply to rajeshalwayswel

I find part of your problem description a bit ambiguous.   You said (bold-italics are mine)

 


 

For a subject if sys, dbp is missing after first record we need to return first value to it missing value to it next records. If first record is also missing for a subject we need to set to missing


 

What do you mean by "first record is also missing"?   That it is missing in addition to a later missing obs?   Do you mean if first obs is missing, then all obs should be set to missing?  Please give an example of desired output:

 

 

Frequent Contributor
Posts: 81

Re: Last observation carrying forward for a subject.

Required Output:

data gd;
input id$ visit sbp dbp;
cards;
001 10 120 90
001 10 110 90
001 10 130 90
001 20 110 90
001 30 110 98
002 10 120 .
002 10 110 80
002 10 130 98
002 20 110 90
002 30 110 98
003 10 120 90
003 10 110 80
003 10 130 98
003 10 110 90
003 20 120 90
003 30 110 98
;
run;

Super User
Posts: 22,850

Re: Last observation carrying forward for a subject.

Posted in reply to rajeshalwayswel

Retain Y;

if not missing(dbp) or first.ID then Y=dbp;

 

Frequent Contributor
Posts: 81

Re: Last observation carrying forward for a subject.

Thanks Reeza....

Ask a Question
Discussion stats
  • 5 replies
  • 170 views
  • 0 likes
  • 3 in conversation