Help using Base SAS procedures

Visit Numbers for Unscheduled Visits

Reply
Contributor
Posts: 32

Visit Numbers for Unscheduled Visits

Hi everyone,

How do I derive vist numbers for unsheduled labs. If an unsheduled lab happens between visit 1 and visit 2 then visit number for that unscheduled visit shoud be 1.1 and if there are 2 unscheduled visits between visit 1 and visit 2, then first unscheduled visit should be 1.1 and second unscheduled visit should be 1.2. and so on.. The following in the sample data. Column VISITNUM should be derived.

Thanks in advance.

SubjectLBCATLBTESTLBDTCVISITVISITNUM
100hematologyRBC10-Jan-11Screening-1
100hematologyRBC10-Mar-11Baseline1
100hematologyRBC15-Mar-11Unscheduled1.1
100hematologyRBC1-Apr-11Unscheduled1.2
100hematologyRBC10-Apr-11Month 12
100hematologyRBC10-May-11Month 23
101hematologyRBC11-Jan-11Screening-1
101hematologyRBC10-Feb-11Baseline1
101hematologyRBC15-Mar-11Unscheduled2
101hematologyRBC1-Apr-11Unscheduled2.1
101hematologyRBC10-Apr-11Month 12.2
101hematologyRBC10-May-11Month 23
Super User
Super User
Posts: 7,392

Re: Visit Numbers for Unscheduled Visits

Well, that depends on what you specs/team tell you.  If it is just sort by date (and what if there are multiple on the same date?), then sort the dataset, retain lstvisit and lstunsch, if unscheduled then visit=lstvisit + lstunsch.  It could however be far more complicated than that with applied logic defining visit windows, or other known patterns.

Code for the above:

data want;

     set have;

     by subjid;

     retain lstvisit lstunsch;

     if first.subjid then do;

          lstvisit=visit;

          lstunsch=0;

     end;

     else if type="UNSCHEDULED" then do;    

          lstunsch=lstunsch+0.1;          /* note I would recommend using 0.01 as there may be more than 10 */

          visit=lstvisit + lstunsch;

     end;

     else lstvisit=visit;

run;

New Contributor
Posts: 2

Re: Visit Numbers for Unscheduled Visits

Hi RW9, I am working on a HW exercise similar to the OP. The solution you posted only partialy worked for me, SAS produces the vector lstvist such that if visit is (1, 2, ., .,3, ., 4) then lstvisit is (1, 2, 2, 2, 3, 3, 4), which is a step in the right direction. However, the lstunsch produces a vector of missing values. I have been trying to debug for a while but have had little success,  any chance you might know what is going on there? Also, I am assuming that your use of "visit" refers to the OP's use of  "visitnum", and "type" refers to  "visit".

Message was edited by: Kevin Adams

Trusted Advisor
Posts: 2,113

Re: Visit Numbers for Unscheduled Visits

Your narrative and your example don't match.  Please restate more precisely.

Subject 101 on 15Mar and 10Apr do not follow the pattern in the narrative. 

New Contributor
Posts: 2

Re: Visit Numbers for Unscheduled Visits

I think this solution should work:

DATA new;

  Set old;

  By subjid;

  Retain visit_r 0;

  If first.subjid Then visit_r = visitnum;

  If visit = "Unscheduled" Then visitnum = visit_r + 0.01;

  visit_r = visitnum;

  Drop visit_r;

RUN;

For a similar example see http://www.mwsug.org/proceedings/2009/stats/MWSUG-2009-D14.pdf

Respected Advisor
Posts: 4,644

Re: Visit Numbers for Unscheduled Visits

Keep it simple:

data want;

set have;

by subject;

if first.subject then visitnum = -1;

visitnum + 0.1;

if VISIT ne "Unscheduled" then visitnum = ceil(visitnum);

run;

PG

PG
Occasional Contributor
Posts: 8

Re: Visit Numbers for Unscheduled Visits

Really helpful! But could you please explain more detailed of the code? I can hardly understand the logic very clearly. Thanks. Smiley Happy 

Occasional Contributor
Posts: 8

Re: Visit Numbers for Unscheduled Visits

Really helpful. Thanks.

Ask a Question
Discussion stats
  • 7 replies
  • 2004 views
  • 4 likes
  • 6 in conversation