BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sam369
Obsidian | Level 7

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

4 REPLIES 4
ballardw
Super User

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.

novinosrin
Tourmaline | Level 20
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.  

novinosrin
Tourmaline | Level 20

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;
sam369
Obsidian | Level 7

Thank you all!!!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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