oraclesasproc-sql

Get data between specific set of months in SAS PROC SQL


I have a dataset where the date is in the 20-JUN-23 format. I want to filter the data such that I can get all the data between 1-MAR-23 and 30-APR-23. If current month is 6 then I want data between 3rd and 4th month. When we enter July (7th month), I want data between April (4th Month) and May (5th Month) and so on. Basically whenever I run the code it gives me data between 3 and 2 months before the current month. I am using SAS PROC SQL (SQL is Oracle is that matters).


Solution

  • The SAS solution uses INTNX and assumes your date is a variable in a SAS date format (numeric with date format).

    proc sql;
    create table want as
    select * 
    from have
    where date between intnx('month', date, -3, 'b') and intnx('month', date, -2, 'e');
    quit;