Dear,
I need to assign values to a variable (avisitnum) based on recent visit when the visit is unscheduled.
Eg. For the OBS 4, the avisitnum value is 2 because recent scheduled visit is scheduled2 and visitnum=2.
For unscheduled visit, the recent visitnum should be populated
data
id period date visit visitnum
1 period1 2015-06-02 scheduled1 1
1 period1 2015-06-07 scheduled2 2
1 period1 2015-06-23 scheduled3 3
1 period1 2015-06-17 unscheduled 99
output needed
id period date visit visitnum avisitnum
1 period1 2015-06-02 scheduled1 1 1
1 period1 2015-06-07 scheduled2 2 2
1 period1 2015-06-23 scheduled3 3 3
1 period1 2015-06-17 unscheduled 99 2
Use:
proc sort data=have; by id period date; run;
data want;
set have;
prev_visitnum = lag(visitnum);
if visit = "unscheduled" then visitnum - prev_visitnum;
/* OR if visitnum=99 then visitnum = prev_visitnum */
drop prev_visitnum;
run;
Hey,
you can use the below code to populate the recent visit on the unscheduled visits.
data have;
length date 8 visit $12;
informat date yymmdd10.;
input id $ period $ date $ visit $ visitnum ;
datalines;
1 period1 2015-06-02 scheduled1 1
1 period1 2015-06-07 scheduled2 2
1 period1 2015-06-23 scheduled3 3
1 period1 2015-06-17 unscheduled 99
run;
proc sort data = have;
by id period date visitnum;
run;
data need(drop=a);
set have;
format date ddmmyyd10.;
retain avisitnum;
by id period date visitnum;
a = avisitnum;
if first.period then avisitnum=1;
else avisitnum+1;
if visitnum=99 then avisitnum=a;
run;
hope this helps!
Manjeet
Out of interest, what are you doing with avisitnum? It looks a bit like your creating ADaM datasets here, you may want to check the CDISC documentation, as it is AVISIT, AVISITN, and there are examples on populating unscheduleds. Its just if you keep that output, avisit looks duplicate.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.