DATA Step, Macro, Functions and more

retain previous missing visits

Accepted Solution Solved
Reply
Regular Contributor
Posts: 171
Accepted Solution

retain previous missing visits

Hi all,

 

i would like to retain the previous values to the missing visits;

 

have:

visit

             dt

SUBJID

testcd

testn

 

visitn

dy

WEEK0

07Dec2015

100

TEST

1

 

2

1

WEEK2

20Dec2015

100

TEST

1

 

4

14

WEEK4

03Jan2016

100

TEST

1

 

5

28

WEEK8

01Feb2016

100

TEST

1

 

6

57

FOLLOW-UP

07Feb2016

100

TEST

1

 

801

63

 

 

want:

visit

             dt

SUBJID

testcd

testn

 

visitn

dy

flg

WEEK0

07Dec2015

100

TEST

1

 

2

1

 

WEEK0

07Dec2015

100

TEST

1

 

3

1

1

WEEK2

20Dec2015

100

TEST

1

 

4

14

 

WEEK4

03Jan2016

100

TEST

1

 

5

28

 

WEEK8

01Feb2016

100

TEST

1

 

6

57

 

WEEK8

01Feb2016

100

TEST

1

 

7

57

1

WEEK8

01Feb2016

100

TEST

1

 

8

57

1

WEEK8

01Feb2016

100

TEST

1

 

9

57

1

WEEK8

01Feb2016

100

TEST

1

 

10

57

1

FOLLOW-UP

07Feb2016

100

TEST

1

 

801

63

 

 

 

 

 

i tried:

data want;
  set have;
   by subjid testn test visitn dt;
  output;
   do visitn=2 to 10;
     flg=1;
     output;
   end;
run;

 

 


Accepted Solutions
Solution
‎04-30-2018 02:01 PM
PROC Star
Posts: 1,584

Re: retain previous missing visits

Posted in reply to novinosrin

To handle by groups

data have;
input visit :$10. date :date7. subjid testcd $ testn visitn dy;
datalines;
WEEK0	7-Dec-15	100	TEST	1		2	1
WEEK2	20-Dec-15	100	TEST	1		4	14
WEEK4	3-Jan-16	100	TEST	1		5	28
WEEK8	1-Feb-16	100	TEST	1		6	57
FOLLOW-UP	7-Feb-16	100	TEST	1		801	63
;

data want;
do _n_= 1 by 1 until(last.subjid);
set have;
by subjid;
__v=visitn;
__dy=dy;
if _n_=1 then call missing(_v, _dy);
else if _n_>1 and visit ne 'FOLLOW-UP' and visitn ne _v+1 then
do;
	do visitn=_v+1  to visitn-1;
	dy=_dy;
	flag=1;
	output;
	end;
end;
flag=.;
visitn=__v;
dy=__dy;
output;
_v=visitn;
_dy=dy;
end;
drop _:;
run;

View solution in original post


All Replies
Super User
Posts: 13,330

Re: retain previous missing visits

For week8 how do we know that we are supposed to stop at visitn of 10? Is 10 going to be the stop for all Subjid?

 

Dare we ask why there is no visitn=1 in the example? What do we do if the first vistn value is greater than 2?

 

Will there be any WeekN visits after Follow-Up for a given Subid? If so you should provide an example of what the desired result might be.

PROC Star
Posts: 1,584

Re: retain previous missing visits

[ Edited ]
data have;
input visit :$10. date :date7. subjid testcd $ testn visitn dy;
datalines;
WEEK0	7-Dec-15	100	TEST	1		2	1
WEEK2	20-Dec-15	100	TEST	1		4	14
WEEK4	3-Jan-16	100	TEST	1		5	28
WEEK8	1-Feb-16	100	TEST	1		6	57
FOLLOW-UP	7-Feb-16	100	TEST	1		801	63
;



data want;
set have;
retain _v _dy;
__v=visitn;
__dy=dy;
if _n_>1 and visit ne 'FOLLOW-UP' and visitn ne _v+1 then
do;
	do visitn=_v+1  to visitn-1;
	dy=_dy;
	flag=1;
	output;
	end;
end;
flag=.;
visitn=__v;
dy=__dy;
output;
_v=visitn;
_dy=dy;
drop _:;
run;

EDIT: Since i am a very lazy person, I didn;t bother retaining all variables in your input dataset. But if you follow the code , you can do so.  

Solution
‎04-30-2018 02:01 PM
PROC Star
Posts: 1,584

Re: retain previous missing visits

Posted in reply to novinosrin

To handle by groups

data have;
input visit :$10. date :date7. subjid testcd $ testn visitn dy;
datalines;
WEEK0	7-Dec-15	100	TEST	1		2	1
WEEK2	20-Dec-15	100	TEST	1		4	14
WEEK4	3-Jan-16	100	TEST	1		5	28
WEEK8	1-Feb-16	100	TEST	1		6	57
FOLLOW-UP	7-Feb-16	100	TEST	1		801	63
;

data want;
do _n_= 1 by 1 until(last.subjid);
set have;
by subjid;
__v=visitn;
__dy=dy;
if _n_=1 then call missing(_v, _dy);
else if _n_>1 and visit ne 'FOLLOW-UP' and visitn ne _v+1 then
do;
	do visitn=_v+1  to visitn-1;
	dy=_dy;
	flag=1;
	output;
	end;
end;
flag=.;
visitn=__v;
dy=__dy;
output;
_v=visitn;
_dy=dy;
end;
drop _:;
run;
Regular Contributor
Posts: 171

Re: retain previous missing visits

Posted in reply to novinosrin

Thank you all!!!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 96 views
  • 0 likes
  • 3 in conversation