YM: Calculating Elapsed Months
 How to:Calculate Elapsed Months

Available Languages: reporting, Maintain

The YM function calculates the number of months between two dates. The dates must be in year-month format. You can convert a date to this format by using the CHGDAT or EDIT function.

 Top of page
Syntax: How to Calculate Elapsed Months
`YM(fromdate, todate, output)`

where:

fromdate

I4YM or I6YYM

Is the start date in year-month format (for example, I4YM). If the date is not valid, the function returns the value 0 (zero).

todate

I4YM or I6YYM

Is the end date in year-month format. If the date is not valid, the function returns the value 0 (zero).

output

Integer

Is the name of the field that contains the result, or the format of the output value enclosed in single quotation marks.

Tip: If fromdate or todate is in integer year-month-day format (I6YMD or I8YYMD), simply divide by 100 to convert to year-month format and set the result to an integer. This drops the day portion of the date, which is now after the decimal point.

 Top of page
Example: Calculating Elapsed Months

The COMPUTE commands convert the dates from year-month-day to year-month format; then YM calculates the difference between the values in the HIRE_DATE/100 and DAT_INC/100 fields:

```TABLE FILE EMPLOYEE
PRINT DAT_INC AS 'RAISE DATE' AND COMPUTE
HIRE_MONTH/I4YM = HIRE_DATE/100; NOPRINT AND COMPUTE
MONTH_INC/I4YM = DAT_INC/100; NOPRINT AND COMPUTE
MONTHS_HIRED/I3 = YM(HIRE_MONTH, MONTH_INC, 'I3');
BY LAST_NAME BY FIRST_NAME BY HIRE_DATE
IF MONTHS_HIRED NE 0
WHERE DEPARTMENT EQ 'MIS';
END```

The output is:

```LAST_NAME     FIRST_NAME  HIRE_DATE  RAISE DATE  MONTHS_HIRED
---------     ----------  ---------  ----------  ------------
CROSS         BARBARA      81/11/02    82/04/09             5
GREENSPAN     MARY         82/04/01    82/06/11             2
JONES         DIANE        82/05/01    82/06/01             1
MCCOY         JOHN         81/07/01    82/01/01             6
SMITH         MARY         81/07/01    82/01/01             6```
 WebFOCUS