Help using Base SAS procedures

Number of days between treatments by clientid and case number

Accepted Solution Solved
Reply
Contributor
Posts: 45
Accepted Solution

Number of days between treatments by clientid and case number

 

Hello SAS Users, 

 

I would like to calculate the number of days between treatments by subtracting the starting date of treatment_1 from the starting date of treatment_2 by clientid and case number. In other words, I expect the first row of each case for every client to be missing. 

 

I successfully calculated the callbacktime, but it is calculated for each row independent of clientid and case number.

 

proc sort data=work.sample out=work.sample;

by clientid case;

run;

 

data sample;

   set sample;

     by clientid case;

     callbacktime=intck('day', lag(startdate), startdate);

run;


Accepted Solutions
Solution
‎02-10-2017 08:05 AM
SAS Employee
Posts: 20

Re: Number of days between treatments by clientid and case number

Can you try this:

proc sort data=work.sample out=work.sample;
by clientid case;
run;
 
data sample;
   set sample;
     by clientid case;
     callbacktime=intck('day', lag(startdate), startdate);
     if first.case then callbacktime=.; /* For each new case set callbacktime to missing */
run;

View solution in original post


All Replies
Solution
‎02-10-2017 08:05 AM
SAS Employee
Posts: 20

Re: Number of days between treatments by clientid and case number

Can you try this:

proc sort data=work.sample out=work.sample;
by clientid case;
run;
 
data sample;
   set sample;
     by clientid case;
     callbacktime=intck('day', lag(startdate), startdate);
     if first.case then callbacktime=.; /* For each new case set callbacktime to missing */
run;
Contributor
Posts: 45

Re: Number of days between treatments by clientid and case number

Yes, I can do this. But isn't that what the by command should be for? Shouldn't the program do that for me?

SAS Employee
Posts: 20

Re: Number of days between treatments by clientid and case number

No.

The By statement will create the automatic variables byvar.first and byvar.last.

And will check to see if the sort order is correct in the input rows.

 

The lag function does not know anything about the By statement so you need to add your own logic as I have shown you.

 

Contributor
Posts: 45

Re: Number of days between treatments by clientid and case number

Thanks! That clarifies things. 

Super User
Posts: 6,936

Re: Number of days between treatments by clientid and case number


GKati wrote:

Yes, I can do this. But isn't that what the by command should be for? Shouldn't the program do that for me?


No. What the by statement does:

- it creates an automatic first. and last. variable for every variable in the by statement to signal a group change

- if used without the notsorted option, it throws an error if the dataset is not sorted properly

It is up to you to use the first. and last. to your logical needs. SAS can't guess what you want to happen at a certain group change (ie with multiple by levels one often does not want to reset all retained variables at every level).

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 10,500

Re: Number of days between treatments by clientid and case number

And for a minor difference instead of

callbacktime=intck('day', lag(startdate), startdate);

since you want days

callbacktime = dif(startdate);

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 229 views
  • 1 like
  • 4 in conversation