Home Forums Main Forums SAS Forum Interval Functions INTNX and INTCK

  • Interval Functions INTNX and INTCK

     Datura updated 3 years, 9 months ago 1 Member · 2 Posts
  • Datura

    Member
    February 2, 2021 at 6:49 pm

    The SAS interval functions INTNX and INTCK perform calculations with date values, datetime values, and time intervals. They can be used for calendar calculations with SAS date values to increment date values or datetime values by intervals and to count time intervals between dates. They are very useful in our real work.

    1. The INTNX function increments dates by intervals. INTNX computes the date or datetime of the start of the interval a specified number of intervals from the interval that contains a given date or datetime value.

    The form of the INTNX function is:

    INTNX ( interval, from, n < , alignment > ) ;

    The arguments to the INTNX function are as follows:

    interval:
    is a character constant or variable that contains an interval name

    from:
    is a SAS date value (for date intervals) or datetime value (for datetime
    intervals)

    n
    is the number of intervals to increment from the interval that contains the from value
    alignment controls the alignment of SAS dates, within the interval, used to identify output observations. Allowed values are BEGINNING, MIDDLE, END, and SAMEDAY.

    The number of intervals to increment, n, can be positive, negative, or zero.

    For example, the statement NEXTMON=INTNX(’MONTH’,DATE,1) assigns to the variable NEXTMON the date of the first day of the month following the month that contains the value of DATE. Thus INTNX(’MONTH’,’21OCT2007’D,1) returns the date 1 November 2007.

    2. The INTCK function counts the number of interval boundaries between two date values or between two datetime values. The form of the INTCK function is:
    INTCK ( interval, from, to ) ;

    The arguments of the INTCK function are as follows:

    interval
    is a character constant or variable that contains an interval name.

    from
    is the starting date value (for date intervals) or datetime value (for
    datetime intervals)

    to
    is the ending date value (for date intervals) or datetime value (for
    datetime intervals)

    For example, the statement NEWYEARS=INTCK(’YEAR’,DATE1,DATE2) assigns to the variable NEWYEARS the number of New Year’s Days between the two dates.

    * Incrementing Dates by Intervals
    * Alignment of SAS Dates
    * Computing the Width of a Time Interval
    * Computing the Ceiling of an Interval
    * Counting Time Intervals
    * Checking Data Periodicity
    * Filling In Omitted Observations in a Time Series Data Set
    * Using Interval Functions for Calendar Calculations

    • This discussion was modified 3 years, 9 months ago by  Datura.
  • Datura

    Member
    February 2, 2021 at 6:55 pm

    Example: How to find all the Friday dates in a given month ??

    A could be any given date, Friday_Date is the dates of all the Fridays in the same MONTH as A.

     data BBB;
    A="06JAN2019"d;
    B=intnx("week", A, 0);
    C=intnx("day", B, 5);

    Do I=-5 to 5;
    Friday_Date=C+I*7;
    format B C Friday_date date9. ;
    if month(Friday_Date)=month(A) then output;
    end;
    run;

    Summarize the total number of Friday dates in a month

    data C;
    set BBB END=EOF NOBS=N;
    DD=Friday_date-A;
    If DD GE 0;
    run;

    Data D;
    set C End=EOF NOBS=N;
    call symput("Num_Friday", put(N,1.));
    %put Number of Friday in this month is *&Num_friday*;
    run;

    Proc sql;
    select Friday_date INTO: Friday1-:Friday&Num_friday
    from D;
    quit;
    %put _user_;
    • This reply was modified 3 years, 9 months ago by  Datura.

Log in to reply.

Original Post
0 of 0 posts June 2018
Now