the goal is to write a SAS program to make diagnosis on a disease. idno = id of patient, visit = visit number, test = result from test (can be A or B),
to make a positive diagnosis, the patient have to have test=A in two consecutive visits. so if test=A in visit 1 and test=A in visit 2, then patient is positive. If A in visit 1 and B in visit 2, patient is negative.
problem occurs when there's visits with missing data. For example, if patient 11 tested A in visit 3, did not get tested in visit 4, and tested again A in visit 5, he should be positive (because technically he was tested A 2 times consecutively).
note, once a patient is labeled as positive, he remains positive forever, regardless of later results
idno visit test
11 1 A
11 2 B
11 3 A
11 4
11 5 A = positive
idno visit test
11 1 A
11 2
11 3 A
11 4 B
11 5 B = positive
idno visit test
11 1 A
11 2
11 3 B
11 4
11 5 A = negative
thank you for the help! here's the test code to start with:
data test;
input idno visit test $;
datalines;
11 1 A
11 2 B
11 3 A
11 4 .
11 5 A
;
run;
Hi,
Try the code below:
data test;
input idno visit test $;
datalines;
11 1 A
11 2 B
11 3 A
11 4 .
11 5 A
;
run;
data want;
length diag $ 10;
set test (where=(not missing(test)));
by idno;
if not first.idno and test=lag(test) then diag='positive';
proc print;run;
Linlin, I think you will have a problem with consecutive values of B. Here's an alternative:
data want;
set have;
by idno;
length result $ 8;
retain result;
if first.idno then do;
A_count=0;
result=' ';
end;
if test='A' then A_count + 1;
else if test='B' then A_count=0;
if A_count > 1 then result='Positive';
run;
This identifies the "Positive" subjects only, but that's easy to change if needed. It also shows when they first were designated as "Positive".
Hi Astounding,
I thought it should be "Positive" when there are consecutive values of B.
Thanks - Linlin
Oh, My, can't you be sharper? Astounding, you have been a huge asset to this forum, and I am among one of those who have been benefited the most. I have been a big fan since 2 years ago when I first finished one of your books on SAS program efficiency.
Haikuo
Thanks for the more than kind words. We all learn here. Between you and KSharp, I'm actually learning something about hash tables. Grrrrr. Watch out, the old dog has a new trick.
: You're not the only old dog who continues to learn new tricks! Not a play on your screen name, but I've always been astounded that more people don't take advantage of how much help and knowledge sites like this forum, and SAS-L, have to offer. After almost 40 years working with SAS, I still learn something new every day.
LinLin's code is surely working for you, but In case you want to keep all of the rows and just add a variable, here is another way:
data test;
input idno visit test $;
datalines;
11 1 A
11 2 B
11 3 A
11 4 .
11 5 A
;
data want;
do until (last.idno);
set test;
by idno;
length flag _r $10.;
if test=_r and _r='A' then _c+1;
if _c>=1 then flag='Positive';
_r=coalescec(test, _r);
output;
end;
call missing (of _:);
drop _:;
run;
Haikuo
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.
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.