Quantcast
Channel: ListenData
Viewing all articles
Browse latest Browse all 425

Intermediate Proc SQL Tutorial

$
0
0
This article explains practical application of SQL queries with examples.

Example 1 : Creating trend variables
Sample Dataset
data example1;
input ID Months Revenue Balance;
cards;
1011390
10123368
10132251
1014390
10153365
10162254
102110018
10225862
10239597
102410018
10255865
10269592
;
Task : Calculate total revenue and total balance accumulated in the first 3 months and how much the data spreads in the first 3 months time period.
proc sql noprint;
create table output1 as
select ID,
sum(case when 1 <= Months <= 3 then Revenue else . end) as Rev_1_3,
sum(case when 1 <= Months <= 3 then Balance else . end) as Bal_1_3,
var(case when 1 <= Months <= 3 then Revenue else . end) as Var_Rev_1_3,
var(case when 1 <= Months <= 3 then Balance else . end) as Var_Bal_1_3
from example1
group by ID;
quit;

Output : 
Output

Example 2 : Extract First and Last Observation within a Group

PROC SQL : Alternative to First. Statement
proc sql;
create table output2 (drop=n) as
select *, monotonic() as n
from example1
group by ID
having min(n) = n;
quit;
PROC SQL : Alternative to Last. Statement
proc sql;
create table output2 (drop=n) as
select *, monotonic() as n
from example1
group by ID
having max(n) = n;
quit;
Example 3 : Self Join
Suppose you have data for employees. It comprises of employees' name, ID and manager ID. You need to find out manager name.

Proc SQL : Self Join
data example2;
input Name $ ID ManagerID;
cards;
Smith123456
Robert456 .
William222456
Daniel777222
Cook383222
;
run;
SQL Query : Self Join
proc sql;
create table want as
select a.*, b.Name as Manager
from example2 as a left join example2 as b
on a.managerid = b.id;
quit;
Example 4 : Capping Extreme Values
Suppose you need to cap values of a column.

data have1;
input x y z;
cards;
101  1 10
102  2 20
103  3 45
104  1 23
105  2 42
106  3 46
107  1 61
109  2 22
110  3 28
111  1 30
112  2 32
113  3 39
;
run;
proc sql noprint;
create table output2 (drop=z rename= (z1=z))  as
select *, case when z >=  40 then 40 else z
end as z1 from have1;
quit;
First cap values of column z and then sum all the z values by column y.
proc sql;
select sum(z1) as OutCol
from (select *, case when z >=  40 then 40 else z
end as z1 from have1)
group by y;
quit;

Example 5 : Select All Excluding 1 Variable
proc sql;
create table want (drop=x) as
select a.*,b.*
from dat1 a, dat2 (rename=(id=x)) b
where a.id = b.x;
quit;
Example 6 : Sub Query - ANY and ALL Operators

1. ANY operator selects values that pass the comparison test with any of the values that are returned by the sub-query. 

2. ALL operator selects values that pass the comparison test with all of the values that are returned by the sub-query. 
data jansale;
input sale id;
cards;
1001
105 2
1083
1104
;
run;
data febsale;
input sale id;
cards;
1201
105 2
1183
1174
;
run;
proc sql;
select *
from jansale
where sale < all (select sale from febsale);
quit;
proc sql;
select *
from jansale
where sale < any (select sale from febsale);
quit;


Viewing all articles
Browse latest Browse all 425

Trending Articles