How to calculate age from date of birth in OpenEdge ABL? - openedge

I am new to progress and I want to calculate age from the date of birth and I have no idea how to do it. If anyone knows about this please help me.
Thanks in advance.
What I have tried so far is:
define var dob as date.
define var age as character.
assign
dob = 09/16/1988.
age = STRING(INT(YEAR(TODAY) - YEAR(dob ))).
message age view-as alert-box.
it show age 30 but in real the age is 29.

Use the interval function.
define var dob as date initial 09/16/1988.
message interval( today, dob, "years" ) view-as alert-box.
Returns 29 (as long as today is before the 16th of this year) - handles leap years fine too.

In your code you are just count years, that is not the correct way. It will be shown just difference of the year. You need to count years based on month and days also. So, you can try the following code that will work also for the leap year.
define var dob as date.
define var vYears as int.
define var age as int.
assign
dob = 09/16/1988.
vYears = int(year(today) - year(dob)).
if (month(today) < month(dob)) then do:
age = vYears - 1.
end.
if (month(today) = month(dob)) then do:
if (day(today) < day(dob)) then do:
age = vYears - 1.
end.
else do:
age = vYears.
end.
end.
if (month(today) > month(dob)) then do:
age = vYears.
end.
message age view-as alert-box.

Something you can do in ABL is to subtract dates. which gives the age in days. You could then divide that by 365 and round it down to give the age in years. Obviously this doesn't take leap years into account, so isn't 100% accurate.
DEFINE VARIABLE dob AS date.
DEFINE VARIABLE age AS INTEGER.
ASSIGN
dob = 09/16/1988
age = TRUNCATE(((TODAY - 03/25/1979) / 365),0).
MESSAGE age VIEW-AS ALERT-BOX.
Alternatively, if you want it to be accurate including leap years you have to work a bit harder.
What I've done here is to add the number of years to the birth date to see if the birthday has already happened. If it hasn't then I take 1 year away. There's probably more elegant solutions, but it will do the job!
DEFINE VARIABLE dob AS date.
DEFINE VARIABLE age AS INTEGER.
DEFINE VARIABLE comp AS DATE NO-UNDO.
ASSIGN
dob = 09/16/1988.
comp = add-interval(dob,year(TODAY) - year(dob),"YEARS").
IF TODAY GT comp THEN
age = year(TODAY) - year(dob).
ELSE
age = year(TODAY) - year(dob) - 1.
MESSAGE age VIEW-AS ALERT-BOX.

Related

Bizdays doesn't exclude weekends

I am trying to calculate utilization rates by relative employee lifespans. I need to assign a total number of hours available to this employee between the earliest and furthest date in which time was recorded. From there I will use this as the divisor in utilization rate = workhours / totalhours.
When testing the bizdays function, I tried a simple example.
bizdays::bizdays("2020-02-07","2020-02-14")
[1] 7
Any reason why the function is not returning the correct number of business days?
I am expecting 5 business days since 2/07 was a Friday so only 1 week should be included.
The goals is to use bizdays in the following function that will be applied to a grouped df with gapply.
timeentry = function(x){
end_date = max(x$terminus)#creates an end_date variable from further end date in the group
start_date = min(x$onset) #creates a start_date from earliest start date in the group
start_date %>% bizdays(end_date) * 8 #subtracts dates and multiple by 8 to get work hours between two dates
}
I will apply the function in this manner. Unfortunately, it returns an error suggesting it cannot allocate vector of size 4687 gb. This is a separate issue I hope someone can point out.
util = group %>% gapply(.,timeentry)
where group is the grouped df.
Try setting up your calendar with create.calendar
library(bizdays)
create.calendar(name = "demo", weekdays = c("saturday", "sunday"))
bizdays::bizdays("2020-02-07","2020-02-14", cal = "demo")
[1] 5

Return all with birthdate occurring after death date

I have a dataset of people. For each individual they have a name, their date of birth (DOB), and their date of death (DOD). Some of the DODs occur before the DOB, which is obviously an error. How can I tell R to return all individuals with a DOB occurring after the DOD?
Let's say you have your data stored in df. In order to display the rows where DOB happens after DOD:
df[df$DOB > df$DOD, ]
In order to filter out the errors, do the following:
df[df$DOB <= df$DOD, ]

eeptools age_calc() error: End date must be a date after date of birth

I'm using age_calc() from the eeptools package in R but am getting the error
End date must be a date after date of birth
Both the dob and end date are date class objects with format %yyyy-%mm-%dd
x$age<- floor(age_calc(dob = x$Date.of.Birth2, enddate = x$DisbursalDate2,
units = "years", precise = T))
I can see that this question is old now, but just in case anybody else comes across this looking for an answer.
The coding behind the age_calc() function uses the following logic:
if (any(enddate < dob)) {
stop("End date must be a date after date of birth")
}
So you are getting the error because at least one of your enddate values ('DisbursalDate2' in your case) is before the dob value ('Date.of.Birth2' in your case). To find out what is causing the error, just do:
subset(x, x$DisbursalDate2 < x$Date.of.Birth2)

Get RowCount With Date Comparasion in SSRS

I am new to SSRS.
I have a dataset, my dataset brings data from a stored procedure.
one of the parameters of my sp is StartDate and another one is EndDate. Their type is datetime
And the table has a dateTime Column called Date.
I have two gauges and I wanna bind integer values to my gauges.
First one is the count of rows where Date < DateAdd(DateInterval.Hour,24,StartDate)
and te second is count of rows where Date > DateAdd(DateInterval.Hour,24,StartDate)
How will I write the exact script. Whatever I wrote is not working.
I appreciate any help, thanks.
You need to set the gauge Pointer value as something like:
=Sum(IIf(DateDiff(DateInterval.Day, Parameters!StartDate.Value, Fields!Date.Value) >= 1
, 1
, 0))
This is counting rows where the time difference is less than a day compared to the parameter StartDate. Just change it slightly to get those where the difference is at least a day:
=Sum(IIf(DateDiff(DateInterval.Day, Parameters!StartDate.Value, Fields!Date.Value) >= 1
, 0
, 1))
Worked fine for me in a quick test:

Subtract 1 year from datetime

I have seen a lot of info on how to subtract one datetime from the other and how to add years on to a datetime. but the following is giving me a headache....
When a user inserts a record a hidden field called subdate with a value of datetime.now is added to the db.
I then need to have an 'old records' page that lists all the entries that are over 1 year old and was hoping to use something (but using subtract method) similar to;
(DateTime.Now.AddYears(1))
but there is no SubtractYears available? Why?
Please can you let me know how I achieve the same result?
DateTime.Now.AddYears(-1)
From the documentation:
A number of years. The value parameter can be negative or positive.
now = datetime.now()
last_year = (now.year - 1)
datestr = (datetime.strptime(str(now.year - 1), "%Y")).strftime("%y")
print(f"Last Year: {datestr}")
The output will be:
Last Year: 20
If you prefer to have four digit year then change %y to %Y

Resources