I have to compare nine different obs term dates to find the one that was documented most recently, and return that value.
The nine different obs terms are the following:
SW_CURRENT_SMIDP
SW_CURRENT_SMIPB
SW_CURRENTSMIIPA
SW_CURRENTSMITPR
SW_CURRENTSMICTP
SWCURRENTSMIEPS
SW_CURRENTSMIIMH
SWCURRENTSMIHSWN
SW_CURRENTSMIIIS
I have tried looking through loops and array posts, but this one is over my head. If anybody can help with some sample code if you've done something similar that would be amazing.
Thanks in advance for any help.
Crystal
Hello,
The function below may help. I haven't had a chance to debug this, but it will at least get you started. I took the first term in your list and assumed it was the correct value. Then checked all of the other terms and replaced them with the most recent if needed. You didn't mention what to do if there are two terms that were collected on the same day. In the function below it just takes the last value in the list in that case. I hope this helps.
Brad
fn fnDateSort()
{
//1. two arrays - one obs names, the other the dates
//2. assume the first date is the correct value and loop through the other values
//3. replace as needed if any of the dates are more recent
local goodDate = LAST_SIGNED_OBS_DATE("SW_CURRENT_SMIDP")
local goodValue = LAST_SIGNED_OBS_VALUE("SW_CURRENT_SMIDP")
local termArray = array("SW_CURRENT_SMIPB","SW_CURRENTSMIIPA","SW_CURRENTSMITPR",
"SW_CURRENTSMICTP","SWCURRENTSMIEPS","SW_CURRENTSMIIMH",
"SWCURRENTSMIHSWN","SW_CURRENTSMIIIS")
for a = 1, a <= size(termArray), a = a+1 do
if (DURATIONDAYS(goodDate,LAST_SIGNED_OBS_DATE(termArray[a]))>0) then //returns a positive value if the date checked is more recent
goodDate = LAST_SIGNED_OBS_DATE(termArray[a])
goodValue = LAST_SIGNED_OBS_VALUE(termArray[a])
else
//keep current values
""
endif
endfor
return goodValue
}
Thanks bhoover this is a great start!
I'm only having two issues now.
1) If the first obs term SW_CURRENT_SMIDP is not filled out but others are then I don't get anything returned. There is not one specific obs term that would always be filled out.
2) If there are two obs terms added the same day can it take into account the time and pull the one that was added more recently?
Regarding your first point the updated code below may help. It now no longer assumes that there will be a value in the first position. It will check every term until it finds a value to start checking the others against. If it does not find any values in any of the obs terms it will return a blank.
Regarding the time. You can modify the function below to use LAST_SIGNED_OBS_DATETIME(). I would probably just write separate functions to return the date and time separately. However, I'm not sure how useful it would be. Typically most of our providers sign their visits and all the terms at the same time, so they would have the same time stamp. However the workflow at your clinic may be different. I hope the function below helps.
Brad
fn fnDateSort()
{
//1. two arrays - one obs names, the other the dates
//2. check every value and use the first one that isn't null
//3. replace as needed if any of the dates are more recent
local goodDate = ""
local goodValue = ""
local termArray = array("SW_CURRENT_SMIDP","SW_CURRENT_SMIPB","SW_CURRENTSMIIPA","SW_CURRENTSMITPR", "SW_CURRENTSMICTP","SWCURRENTSMIEPS","SW_CURRENTSMIIMH", "SWCURRENTSMIHSWN","SW_CURRENTSMIIIS")
for a = 1, a <= size(termArray), a = a+1 do
//make sure we have a starting date
if (goodDate == "") then
if (LAST_SIGNED_OBS_DATE(termArray[a]) <> "") then
goodDate = LAST_SIGNED_OBS_DATE(termArray[a])
else
""
endif
else if (DURATIONDAYS(goodDate,LAST_SIGNED_OBS_DATE(termArray[a]))>0) then
//returns a positive value if the date checked is more recent
goodDate = LAST_SIGNED_OBS_DATE(termArray[a])
goodValue = LAST_SIGNED_OBS_VALUE(termArray[a])
else
//keep current values
""
endif
endif
endfor
return goodValue
}
Brad,
Thank you so much. Your second response is exactly what I need.
Thanks,
Crystal
Glad I could be helpful.