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

I am trying to find the closest date (date1-date680 date9.) to GFR_date (date9.) for each person (ID).

 

I tried the code below, which would work if GFR_date was a date constant (https://blogs.sas.com/content/sgf/2014/11/21/sas-arrays-be-not-afraid/) but in my case GFR_date is different for each person.

 

data results (keep = ID closest diff GFR_date);
format ID 3. date1-date680 closest date9.;
set egfr;
array date[*] date1-date680;
closest = date[1];
diff = abs(date[1] - GFR_date);
do i = 2 to dim(date);
if abs(date[i] - GFR_date) < diff then do;
closest = date[i];
diff = abs(date[i] - GFR_date);
end;
end;
run;

 

Any help would be much appreciated. Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Probably the data is wrong, and some of the date variables have missing values.  This statement needs to change:

 

if abs(date[i] - GFR_date) < diff then do;

If one of the dates is missing, this generates a missing value which is certainly less than DIFF.  So the final DIFF value would be missing.

 

Try this instead:

if . < abs(date[i] - GFR_date) < diff then do;

View solution in original post

2 REPLIES 2
PGStats
Opal | Level 21

It should work just as well. What problem did you encounter?

PG
Astounding
PROC Star

Probably the data is wrong, and some of the date variables have missing values.  This statement needs to change:

 

if abs(date[i] - GFR_date) < diff then do;

If one of the dates is missing, this generates a missing value which is certainly less than DIFF.  So the final DIFF value would be missing.

 

Try this instead:

if . < abs(date[i] - GFR_date) < diff then do;

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
  • 2 replies
  • 777 views
  • 1 like
  • 3 in conversation