Hi everyone,
I am trying to calculate number of years from baseline visit by subjects. Date format is mmddyy10.
When I try the code below, it does the job for the first visit date (baseline) but not for the
following visit dates (see results table at the end):
proc sort data= have;
by ID Test_Date;
run;
data want;
set have;
length Visit 8.;
by ID Test_Date;
retain first.Test_Date;
if first.ID then do;
first.Test_Date = Test_Date;
end;
Visit = intck('year',first.Test_Date,Test_Date)+1;
format first.Test_Date Test_Date mmddyy10.;
run;
ID | Test_Date | Visit |
1 | 10/25/2011 | 1 |
1 | 11/05/2013 | 54 |
1 | 11/03/2015 | 56 |
1 | 11/07/2017 | 58 |
2 | 05/04/2011 | 1 |
3 | 06/02/2011 | 1 |
3 | 06/10/2013 | 54 |
3 | 06/15/2015 | 56 |
3 | 06/08/2017 | 58 |
Any suggestion will be much appreciated,
Thank you!JJ
proc sort data= have;
by ID Test_Date;
run;
data want;
set have;
length Visit 8.;
by ID Test_Date;
retain first_Test_Date;
if first.ID then do;
first_Test_Date = Test_Date;
end;
Visit = intck('year',first_Test_Date,Test_Date)+1;
format first_Test_Date Test_Date mmddyy10.;
run;
You can't quite use first. in that manner, instead create a new variable.
proc sort data= have;
by ID Test_Date;
run;
data want;
set have;
length Visit 8.;
by ID Test_Date;
retain first_Test_Date;
if first.ID then do;
first_Test_Date = Test_Date;
end;
Visit = intck('year',first_Test_Date,Test_Date)+1;
format first_Test_Date Test_Date mmddyy10.;
run;
You can't quite use first. in that manner, instead create a new variable.
That worked perfectly!
Thank you!
Values of the variables FIRST.Test_date (or Last.Test_date or any first / last variable) will only ever be 1 and 0. (the dot is important). The first and last also get reset at each read of the data so would be pretty useless in INTCK.
You want to retain a variable First_test_date and use that in your calculation.
Also the automatic variables First. and Last. are not written to the output data set. Did you notice that?
I did notice first. variable was not in the output, now I understand why.
Thank you for the information it is very much appreciated.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.