DATA Step, Macro, Functions and more

Assigning values to a variables based on recent visitnumber

Reply
Super Contributor
Posts: 272

Assigning values to a variables based on recent visitnumber

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

Trusted Advisor
Posts: 1,554

Re: Assigning values to a variables based on recent visitnumber

Posted in reply to knveraraju91

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;

Contributor
Posts: 52

Re: Assigning values to a variables based on recent visitnumber

Posted in reply to knveraraju91

 

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
Super User
Super User
Posts: 7,942

Re: Assigning values to a variables based on recent visitnumber

Posted in reply to knveraraju91

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.

Ask a Question
Discussion stats
  • 3 replies
  • 179 views
  • 3 likes
  • 4 in conversation