BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

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

3 REPLIES 3
Shmuel
Garnet | Level 18

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;

mnjtrana
Pyrite | Level 9

 

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;

 

Untitled.jpg

hope this helps!

Manjeet


Cheers from India!

Manjeet
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1630 views
  • 3 likes
  • 4 in conversation