BookmarkSubscribeRSS Feed
Davidliu494
Calcite | Level 5

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;

7 REPLIES 7
Linlin
Lapis Lazuli | Level 10

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;

Astounding
PROC Star

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".

Linlin
Lapis Lazuli | Level 10

Hi Astounding,

I thought it should be "Positive" when there are consecutive values of B.

Thanks - Linlin

Haikuo
Onyx | Level 15

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

Astounding
PROC Star

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.  Smiley Happy

art297
Opal | Level 21

: 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.

Haikuo
Onyx | Level 15

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 789 views
  • 0 likes
  • 5 in conversation