BookmarkSubscribeRSS Feed
nmkz
Calcite | Level 5

Hello there,

I have data of when hypothetical patients received a certain type of surgery. The type of surgery is denoted by the variable 'condx'. The data states what date the patient received the surgery and how old the they were on that date. Some patients had more than one procedure and on different days, but the patients' ages on those following dates are missing. I would like to know how to use AGE and EVENT_DATE on the first patient record in order to calculate AGE on later dates by comparing EVENT_DATEs. I am trying to put those missing ages in a new column I named "DIFF". I used the following code:

 

.

 

RETAIN AGE;
IF LAST.AGE= . THEN AGE=DIFF;
DIFF= LAST.EVENT_DATE-FIRST.EVENT_DATE

RUN;
PROC PRINT DATA=PROJECT.COMPLETE;
RUN;

 

 

My initial assumption was to find the difference in the dates which would give me the amount of days between dates, however I don't believe this is correct. Thanks in advance for any input.

3 REPLIES 3
PaigeMiller
Diamond | Level 26
data want;
    set have;
    retain dob;
    if first.pt_id then dob=event_date - round(age*365.25);
    if missing(age) then age=(event_date - dob)/365.25;
run;

 

This is untested code, as we cannot test code against data provided in screen captures. We would need data provided in SAS data step code (instructions) if you want tested code.

--
Paige Miller
nmkz
Calcite | Level 5

Thanks for responding. I may not need code tested at this moment. I more so wanted to know what is the correct approach to this situation/problem.

Tom
Super User Tom
Super User

Since FIRST. and LAST. flags are boolean the difference between than could only ever result in values of -1,0 or 1.

 

If you want to remember the first date then you need to retain it.

data want;
  set have;
  by pt_id event_date;
  if first.pt_id then first_date = event_date;
  format event_date mmddyy10.;
  diff = event_date - first_date;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 367 views
  • 0 likes
  • 3 in conversation