BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GKati
Pyrite | Level 9

 

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;

1 ACCEPTED SOLUTION

Accepted Solutions
MichaelLarsen
SAS Employee

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

6 REPLIES 6
MichaelLarsen
SAS Employee

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;
GKati
Pyrite | Level 9

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

MichaelLarsen
SAS Employee

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.

 

GKati
Pyrite | Level 9

Thanks! That clarifies things. 

Kurt_Bremser
Super User

@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).

ballardw
Super User

And for a minor difference instead of

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

since you want days

callbacktime = dif(startdate);

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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