Find nearest value among multiple variables

#1
Hi all,

Kindly request some help with the following:

My dataset includes many date variables, and I would like to find the variable which is nearest to 1 specific date variable (=treatment in example below). Is there a way to achieve this (like for example variable Nearest below)? And if so, how?

Example: (Date=ddmmyyyy)
id treatment QA1 QA2 Q3 Nearest
case1 04-08-2020 02-07-2020 20-08-2020 13-09-2020 20-08-2020
case2 09-08-2020 10-08-2020 30-09-2020 09-12-2020 10-08-2020

This is my first post so if additional information is required, please let me know.

Thank you in advance for your help.

Kind regards,
Lotte
 

katxt

Active Member
#4
Sorry, I have zero experience with SPSS programming and you will probably need a user defined function. If it is a one off job, then I would dump the data into Excel, use the formula, and then copy the answer back again. If it is going to happen regularly, then perhaps some other talkstats member can write you something. It's easy in principle - scan across the possible dates, recording as you go the smallest absolute difference so far, and its matching closest date so far.
Or perhaps you could visit a specialist SPSS programming forum.