Tuesday, August 16, 2011

SQL Query Trick

Table A: customer_id
Table B: customer_id, visit_date, purchase_amt

proc sql;
  create table ab as
  select a.customer_id, b.visit_date, b.purchase_amt
  from a left join b on a.customer_id = b.customer_id
  where b.visit_date between '01jan2011' and '31jan2011'
; quit;

Correct way:
-------------------------
data b1;
  set b;
  if visit_date >= '01jan2011'd and '31jan2011'd;
run;


proc sql;
  create table ab as
  select a.customer_id, b.visit_date, b.purchase_amt
  from a left join b on a.customer_id = b.customer_id
; quit;