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.
Subject | LBCAT | LBTEST | LBDTC | VISIT | VISITNUM |
100 | hematology | RBC | 10-Jan-11 | Screening | -1 |
100 | hematology | RBC | 10-Mar-11 | Baseline | 1 |
100 | hematology | RBC | 15-Mar-11 | Unscheduled | 1.1 |
100 | hematology | RBC | 1-Apr-11 | Unscheduled | 1.2 |
100 | hematology | RBC | 10-Apr-11 | Month 1 | 2 |
100 | hematology | RBC | 10-May-11 | Month 2 | 3 |
101 | hematology | RBC | 11-Jan-11 | Screening | -1 |
101 | hematology | RBC | 10-Feb-11 | Baseline | 1 |
101 | hematology | RBC | 15-Mar-11 | Unscheduled | 2 |
101 | hematology | RBC | 1-Apr-11 | Unscheduled | 2.1 |
101 | hematology | RBC | 10-Apr-11 | Month 1 | 2.2 |
101 | hematology | RBC | 10-May-11 | Month 2 | 3 |
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;
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
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.
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
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
Really helpful! But could you please explain more detailed of the code? I can hardly understand the logic very clearly. Thanks. 🙂
Really helpful. Thanks.
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
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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.
Ready to level-up your skills? Choose your own adventure.