I am trying to trigger a visibility region if obsany("SMOK YR Q") >= 15 years ago and am having trouble. Has anyone created something like this and if so could you please assist me?
Thanks
I can probably help but need more specifics. Firstly, what information are you capturing in the SMOK YR Q OBS term? I'm guessing it is the year they quit smoking? Most OBS terms store information as a textual value so you often have to convert the value to numeric before comparing to another number. For example:
val(OBSANY("SMOK YR Q")) > [some number]
Sunce [some number] is a calculation involving today's date, you will need to perform a date calculation using something like DURATIONDAYS() and then compare result to the total number of days contained in 15 years (15x365). However, if you are only storing the 4 digit year in the OBS term, then you simply need to calculate the difference in the year recorded and current year. Remember when performing calculations with dates, you must first convert them to a string if they are not in that format. Below is an example of calculating the number of years between recorded year and current year. You want to use the string function that will allow you to extract a certain number of characters from a starting position. If memory serves me, it is either getstr() or substr()
local currentyr = sub(str(._TODAYSDATE),7,4)
This should give you the last 4 characters of the string mm/dd/yyyy starting at the seventh character. You can then perform a calculation to get the number of years between the dates as follows.
val(currentyr) - val(OBSANY("SMOK YR Q") )>= 15
Specific functions may be wrong but the concepts are valid. Let me know if you need further assistance.
Shauna,
I’ve sent you a working version of the form. Here is what I have done:
When the form is opened, a watcher expression checks to see if the smoking cessation date has ever been recorded, and if so, it executes a function to calculate the number of years the person has not been smoking. If the timespan is less than 15 years, then the checkbox gets checked. If the timespan is greater than 15 years or the smoking cessation date has never been recorded, then the checkbox is left blank.
I removed the visibility region that was displaying the “SMOK YR Q” OBS term and replaced it with a data display labeled “More Info” that will display the date that has been recorded for the cessation date if it exists; otherwise is will display “Smoking cessation date not recorded.” This is useful for troubleshooting purposes and might be helpful information for the individual completing the form. Regardless, you can delete that control if it is not needed.
Here is the code I used to accomplish what you requested for the benefit of other users:
/* WATCHER EXPRESSION */
{ //Run function when form opens to check if patient has smoked within last 15 years
!if OBSPREV("SMOK YR Q") <> "" then
CheckSmokStat()
else
endif
}
/* GENERAL FUNCTIONS */
//Check to see if patient has a smoking cessation date within the past 15 years and
//automatically check the current smoker checkbox if so, otherwise leave it blank
{fn CheckSmokStat()
{
local quityr = OBSPREV("SMOK YR Q")
local currentyr = sub(str(._TODAYSDATE),7,4)
local deltayr = val(currentyr) - val(quityr)
if (deltayr <= 15) then
//Set checkbox
DOCUMENT.LCS_SMOKER = "Current smoker or one who has quit smoking within the last 15 years"
else
//do nothing, leave checkbox blank
endif
}
}
This works great, thank you! The only thing I had to change was if (deltayr <= 15) then, to: if (deltayr >= 15) then. Thanks again for your help!!
Glad it worked out for you.