Wednesday, 12 October 2011

SAS - Operators SQL


 data plan_2008;
 input plancode $ year plan $ cust;
 cards;
 p101 2008 1min/2rs 450
 p201 2008 2min/3rs 560
 p207 2008 1min/1ps 660
 p345 2008 2min/2rs 640
 p420 2008 1min/80p 713
 ;

data plan_2009;
input plancode $ year plan $ cust;
cards;
p109 2009 1sec/2p 780
p207 2009 1min/1rs 640
p225 2009 1sec/1p 680
p420 2009 1min/80p 750
p431 2009 2sec/1p 790
;

/*operators*/
proc sql;
select * from plan_2008
union all
select * from plan_2009;
quit;

proc sql;
select * from plan_2008
union
select * from plan_2009;
quit;

proc sql;
select plancode,plan from plan_2008
intersect
select plancode,plan from plan_2009;
quit;

/*customisation*/
proc sql;
select plancode,plan,
'available in 2008-2009'as message from plan_2008
intersect
select plancode,plan,
'available in 2008-2009'as message from plan_2009;
quit;

/*except*/
proc sql;
select plancode,plan from plan_2008
except
select plancode,plan from plan_2009;
quit;

/*customization*/
proc sql;
select plancode,plan,
'only in 2008 'as message from plan_2008
except
select plancode,plan,
'only in 2008'as message from plan_2009;
quit;

proc sql;
proc sql;
select plancode,plan,
'only in 2008 'as message from plan_2008
except
select plancode,plan,
'only in 2008'as message from plan_2009;
quit;

proc sql;
(select plancode,plan from plan_2008
except
select plancode,plan from plan_2009)
union all
(select plancode,plan from plan_2009
except
select plancode,plan from plan_2008);
quit;

proc sql;
(select plancode,plan,
'only in 2008 'as message from plan_2008
except
select plancode,plan,
'only in 2008'as message from plan_2009)
union all
(select plancode,plan,
'only in 2009 'as message from plan_2008
except
select plancode,plan,
'only in 2009'as message from plan_2009);
quit;

/*sub queries*/
proc sql;
select * from plan_2008 where plancode not in(select plancode from plan_2009)
union all
select * from plan_2009 where plancode not in(select plancode from plan_2008);
quit;

proc sql;
select * from plan_2008 where plancode  in(select plancode from plan_2009)
union all
select * from plan_2009 where plancode  in(select plancode from plan_2008);
quit;

proc sql;
select * from plan_2009 where plancode  in(select plancode from plan_2008)
select * from plan_2008  where (cust<plan_2009 ,cust);
quit;

No comments:

Post a Comment