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;
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;
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.
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.
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;
Thank you all!!!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.