BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
JJG
Calcite | Level 5 JJG
Calcite | Level 5
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;

 

IDTest_DateVisit
110/25/20111
111/05/201354
111/03/201556
111/07/201758
205/04/20111
306/02/20111
306/10/201354
306/15/201556
306/08/201758

Any suggestion will be much appreciated,

Thank you!
JJ



 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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.

 

View solution in original post

4 REPLIES 4
Reeza
Super User
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.

 

JJG
Calcite | Level 5 JJG
Calcite | Level 5

That worked perfectly!

Thank you!

ballardw
Super User

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?

JJG
Calcite | Level 5 JJG
Calcite | Level 5

I did notice first. variable was not in the output, now I understand why. 

Thank you for the information it is very much appreciated.

 

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

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
  • 4 replies
  • 260 views
  • 2 likes
  • 3 in conversation