BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
JJG
Calcite | Level 5 JJG
Calcite | Level 5

Hello everyone,

 

I have a dataset with subjects (ID) that have the same number of follow-ups (Follow-up) with a result in a test (Test with 0 being negative and 1 being positive).

I want to create a new variable (Test_Positive) with last available follow-up in Test variable. I want the Test_Positive variable to have all follow-ups as 0 or 1 according to last available Test value. The simulation below may be more informative.

Please see below:

 

HAVE:

 

IDFollow-upTest
110
12.
131
141
210
220
230
240
311
321
33.
34.

 

 

WANT:

 

IDFollow-upTestTest_Positive
1101
12.1
1311
1411
2100
2200
2300
2400
3111
3211
33.1
34.1

 

 

I have tried the following code without success (see output at after code):

 

proc sort data = have;
by Follow-up;
run;


data want;
set have;
by Follow-up;
Test_Positive =  . ;
if last.Test then Test_Positive = last.Test;
if last.Test = . then Test_Positive = .;
else Test_Positive = coalesce(Test,Test_Positive);
retain Test_Positive;
run;

 

OUTPUT (the error is in 2 first rows where Test_Positive should be 1):

 

IDFollow-upTestTest_Positive
1100
12.0
1311
1411
2100
2200
2300
2400
3111
3211
33.1
34.1

 

 

Thank you very much for any help!

JJG

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

You need to pass through each ID twice.  In the firstpass find the test_positive value.  In the secondpass keep that value and output:

 

data have;
 input ID	Followup	Test;
datalines;
1	1	0
1	2	.
1	3	1
1	4	1
2	1	0
2	2	0
2	3	0
2	4	0
3	1	1
3	2	1
3	3	.
3	4	.
run;

data want;
  set have (in=firstpass)
      have (in=secondpass) ;
  by id;
  retain test_positive;
  if first.id then test_positive=.;
  if firstpass=1 then test_positive=coalesce(test,test_positive);
  if secondpass=1;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

You need to pass through each ID twice.  In the firstpass find the test_positive value.  In the secondpass keep that value and output:

 

data have;
 input ID	Followup	Test;
datalines;
1	1	0
1	2	.
1	3	1
1	4	1
2	1	0
2	2	0
2	3	0
2	4	0
3	1	1
3	2	1
3	3	.
3	4	.
run;

data want;
  set have (in=firstpass)
      have (in=secondpass) ;
  by id;
  retain test_positive;
  if first.id then test_positive=.;
  if firstpass=1 then test_positive=coalesce(test,test_positive);
  if secondpass=1;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
JJG
Calcite | Level 5 JJG
Calcite | Level 5

That worked out beautifully! 

Much appreciated mkeintz!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 634 views
  • 0 likes
  • 2 in conversation