Search code examples
sqlsasproc

With in statements how do they work in SAS for two different variables?


I want to use the with in statement in SAS but I am not sure how it works with two variables in a where statement with dates. Can anyone give suggestions to the last line of code?

proc sql; 
    create table work.users as 
    select t1.age 
           t1.ID
           t1.DateTime1 
           t2.DateTime2
    from work.database.table t1
    left join work.users2 on datepart(t1.Datetime1) is within 2 days datepart(t2.DateTime2)

Thank you!


Solution

  • Like david25272 said in his comment, within will not work in SAS. You will have to use between:

    proc sql; 
        create table work.users as 
        select t1.age 
               t1.ID
               t1.DateTime1 
               t2.DateTime2
        from work.database.table t1
        left join work.users2 
          on datepart(t1.Datetime1) between datepart(t2.DateTime2)-2 and datepart(t2.DateTime2)+2
    quit;