BookmarkSubscribeRSS Feed
Almighty
Fluorite | Level 6

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
9 REPLIES 9
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

k6adams
Calcite | Level 5

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

Doc_Duke
Rhodochrosite | Level 12

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. 

k6adams
Calcite | Level 5

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

PGStats
Opal | Level 21

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
Emily_Zhang
Fluorite | Level 6

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

Emily_Zhang
Fluorite | Level 6

Really helpful. Thanks.

teja5959
Fluorite | Level 6

Hi i have one more qus for you. in case Unscheduled visit have  in same date 15-mar-11  in your data set present only one Unscheduled visit on that day  

EX;

LBDTC                        visit 

15-Mar-11                  Unscheduled

15-Mar-11                  Unscheduled

15-Mar-11                  Unscheduled

above the data is given i want visitno add 1.1 all three subjects how to do remaining  after the date as taken 1.2 

 

mkeintz
PROC Star

If you want all unscheduled visits on a single day  to have the same visit number,then change 2 lines of @PGStats's code:

 

change

    BY SUBJECT

to

    BY SUBJECT LBDTC

 

and change

    VISITNUM+1
to

    IF FIRST.LBDTC THEN VISITNUM+1;

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

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

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 9 replies
  • 12093 views
  • 4 likes
  • 8 in conversation