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;
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;
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;
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.
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.
Thanks! That clarifies things.
@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).
And for a minor difference instead of
callbacktime=intck('day', lag(startdate), startdate);
since you want days
callbacktime = dif(startdate);
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: