I'm hoping to get some help returning the most recent obs value from 5 different obs terms into a VFE data display.
so far I have been successful at creating an array and sorting it with the following code:
{global surgDateArr=
array(lastobsdate("Surg Dx"),lastobsdate("Surg Dx #1"),lastobsdate("Surg Dx #2"),lastobsdate("Surg Dx #3"),lastobsdate("Surg Dx #4"))
sort(surgDateArr,FALSE)}
In a data display I can see the most recent date is first in the list which belongs to Surg Dx #2 obs term. Now how can I tell it to display the value of that first item?
Thanks in advance for any guidance!
Crystal Price
I believe, the issue is that you can't sort a date properly.
In a false sort where you trying to get the max value in position 1, 02/01/2007 would sort before 01/01/2017 because 02 is greater than 01 in the sort.
I've solved this in the past with a function that transforms the date in YYYY.MM.DD.
Example: Array the obsdate based on "/" and restructure the date with something like this: arrayX[3]+"."+arrayX[1]+"."+arrayX[2] .
Then your array to be sorted will contain strings that can be sorted properly.
When you pull the first value from that array it will still be in the reverse data format.
So array that return on "." then put the date back into American date format with array[2]+"/"+Array[3]+"/"+Array[1].
Swhite,
I believe you have identified an issue I didn't even realize I would have further down the line. Thank you for bringing this to my attention. I understand what you are saying, but unfortunately I have no clue how to build that code you describe. Can you please share the code you used in the past for this if it's available?
Once I can sort the date of the obs terms correctly, I also need to figure out how to display the value of the most recent obs term from the 5 listed above, if anyone has input on that as well please share.
Thanks you so much!
Crystal
This is a sample of the code I used dates in to the yyyy.mm.dd format:
The dateIn parameter is your date in MM/DD/YYYY.
local OutA
OutA = ""
OutA = getfield(dateIn, "/","")
if size(OutA) = 3 then
dateBit = OutA[3] + "." +
(if size(OutA[1]) < 2 then "0" + OutA[1] else OutA[1] endif) + "." +
(if size(OutA[2]) < 2 then "0" + OutA[2] else OutA[2] endif)
else dateBit = dateBit endif
I'll post the reversal back to the American date next.
Here is a sample of the code I used to put the date back into American Date format:
The parameter yymmddDt is your date in YYYY.MM.DD format.
{fn fnAmericanDate(yymmddDt){
local retAmDt
retAmDt = ""
local ZarrayX
ZarrayX = getfield(yymmddDt,".","")
if size(ZarrayX) = 3
then
retAmDt =ZarrayX[z][2] +"/"+ZarrayX[z][3]+"/"+ZarrayX[z][1]
else retAmDt = retAmDt endif
return retAmDt}}
There is another way to look at this too - a sort of modified bubble sort. Modified, because we don't actually care about keeping an ordered list, just which is the most recent value, but the logic is the similar.
1. Start by assuming that the first value is the most recent.
2. Put the date of that value in a temp variable (for explanation we will call this the primary date)
3. Put the value of the value or if you prefer the array reference in a temp variable (we will call this primary value)
4. Check the primary date against the remaining items in the array. You can do this with either durationdays() or date1 > date2
a. if primary date is older than the item being checked against it becomes the primary date and the primary value is also updated
b. if the primary date is newer than the item being checked against it stays the primary date
c. need to know what you want to do if more than one term has the same date
One you have finished looping through the array the temp values should hold the most recent date and value. You would need to test to see if this is any more efficient than the other method suggested. If you have situations where you are looking through many obs terms this will be slow.
Hello,
I wasn't sure if you got this to work effectively for you. Let me know if you did not. I have a short, simple function that will accomplish this. email me at [email protected]