subjid visitnum i want data
101 1 101 1
101 1.5 101 1.5
101 . 101 1.6
101 . 101 1.7
101 2 101 2
101 . 101 2.1
101 . 101 2.2
101 3 101 3
101 . 101 3.1
101 . 101 3.2
101 4 101 4
101 . 101 4.1
DATA Work.Have;
INPUT subjid visitnum;
CARDS;
101 1
101 1.5
101 .
101 .
101 2
101 .
101 .
101 3
101 .
101 .
101 4
101 .
;
RUN;
DATA WANT;
SET Have;
RETAIN VisitNumWant;
IF visitnum NE . THEN VisitNumWant = visitnum;
ELSE VisitNumWant + 0.1;
DROP visitnum;
RENAME VisitNumWant = VisitNum;
RUN;
Not sure what happens when subjid changes because that is not part of your example. This also assumes that every subjid starts with a non missing visitnum.
I think you should gowith @DanielLangley's solution, but this problem is an excellent vehicle to demonstrate the all variables accessed via a SET (or merge) statement are automatically retained across observations until the next SET/merge statement retrieving the same variables.
So the conditional SETs below, retain the variable _VNUM accross multiple incoming obs:
DATA Work.Have;
INPUT subjid visitnum;
CARDS;
101 1
101 1.5
101 .
101 .
101 2
101 .
101 .
101 3
101 .
101 .
101 4
101 .
;
RUN;
data want (drop=_:);
set have;
if visitnum^=. then set have (keep=visitnum rename=(visitnum=_vnum)) point=_N_;
else _vnum+.1;
visitnum=coalesce(visitnum,_vnum);
run;
The COALESCE function tells SAS to retrieve the first non-missing value from a list of numeric values.
Edited note: This program assumes that the first obs for each subjid has a non-missing visitnum (you only show one subjid). If that assumption is wrong, then the start of one subjid would be contaminated by the end of the prior subjid. You would need something like this, which resets _VNUM to missing every time a new subjid is encountered:
data want (drop=_:);
set have;
if subjid^=lag(subjid) then call missing(_vnum);
if visitnum^=. then set have (keep=visitnum rename=(visitnum=_vnum)) point=_N_;
else if _vnum^=. then _vnum+.1;
visitnum=coalesce(visitnum,_vnum);
@teja5959: See also this thread from August 2018: assign unscheduled visits, in particular my post regarding numeric representation issues. In your example, the new visit number "1.7" (most likely) requires rounding, otherwise it will not be recognized in an IF or WHERE condition like visitnum=1.7.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.