-
Notifications
You must be signed in to change notification settings - Fork 0
/
07 Complex Queries.sas
405 lines (372 loc) · 10.7 KB
/
07 Complex Queries.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
%let path=/courses/d649d56dba27fe300/STA5067/SAS Data;
libname nhanes3 "&path/nhanes3";
/*01 SQL Data Step Merge Mix and Match*/
proc contents data=nhanes3.adult;
run;
proc contents data=nhanes3.mortality;
run;
%let demographics=hsageir hssex DMAETHNR dmaracer dmarethn SDPSTRA6 SDPPSU6 WTPFQX6;
%let lib=work;
proc format lib=&lib;
value f_SEX 1="Male" 2="Female";
/*dmaracer*/
value f_RACER 1="White" 2="Black" 3="Other" 8="Mexican-American of unknown race";
/*DMARETHN */
value f_rethn 1="Non-Hispanic white" 2="Non-Hispanic black" 3="Mexican-American" 4="Other";
/*DMAETHNR*/
value f_ethnr 1="Mexican-American" 2="Other Hispanic" 3="Not Hispanic";
run;
proc sql;
create table &lib..adultdemographics as
select seqn,
hsageir as age ,
hssex as sex format=f_sex.,
dmaracer as race format=f_racer.,
dmarethn as race_ethn format=f_rethn.,
dmaethnr as hispanic format= f_ethnr.,
SDPSTRA6 as strata, /*strata for survey procs*/
SDPPSU6 as cluster, /*cluster for survey procs*/
WTPFQX6 as weight /*weight for survey procs*/
from nhanes3.adult
where seqn in (select seqn from nhanes3.mortality
where eligstat=1)
;
run;
proc contents data=&lib..adultdemographics;
run;
%let demographics=hsageir hssex DMAETHNR dmaracer dmarethn SDPSTRA6 SDPPSU6 WTPFQX6;
%let lib=work;
proc format lib=&lib;
value f_SEX 1="Male" 2="Female";
/*dmaracer*/
value f_RACER 1="White" 2="Black" 3="Other" 8="Mexican-American of unknown race";
/*DMARETHN */
value f_rethn 1="Non-Hispanic white" 2="Non-Hispanic black" 3="Mexican-American" 4="Other";
/*DMAETHNR*/
value f_ethnr 1="Mexican-American" 2="Other Hispanic" 3="Not Hispanic";
run;
proc sql;
create table &lib..adultdemographics as
select seqn,
hsageir as age ,
hssex as sex format=f_sex.,
dmaracer as race format=f_racer.,
dmarethn as race_ethn format=f_rethn.,
dmaethnr as hispanic format= f_ethnr.,
SDPSTRA6 as strata, /*strata for survey procs*/
SDPPSU6 as cluster, /*cluster for survey procs*/
WTPFQX6 as weight /*weight for survey procs*/
from nhanes3.mortality l left join nhanes3.adult r
on l.seqn=r.seqn
where eligstat=1
;
run;
proc contents data=&lib..adultdemographics;
run;
proc contents data=nhanes3.lab;run;
proc means data=nhanes3.lab;run;
%let lib=work;
%let sub=HGP HTP TCP TGP LCP HDP FBPSI CRP SGP URP;
%let u= 88888 88888 888 8888 888 888 8888 88888 888 88888;
%let numvar=10;
proc sql;
create table onmort as
select *
from nhanes3.lab (keep= seqn &sub)
where seqn in (select seqn
from nhanes3.mortality
where eligstat=1)
;
quit;
data &lib..labsubset (drop=i label="Lab subset, Nhanes3, fill values replaced by unknowns");
set onmort;
array vars{*} ⊂
array unk{&numvar} _temporary_ (&u);
do i=1 to dim(vars);
if vars{i}=unk{i} then vars{i}=.;
end;
rename HGP=Hemoglobin
HTP=Hematocrit
TCP=cholesterol
TGP=triglycerides
LCP=ldl
HDP=hdl
FBPSI=fibrinogen
CRP=c_reactive_protein
SGP=glucose
URP=urinary_creatinine;
run;
proc contents data=&lib..labsubset;
run;
/*02 Complex SQL Joins*/
libname orion "&path/orion";
proc sql;
create table avgsalary as
select Job_Title,
avg(Salary) as Job_Avg format=comma7.
from orion.Employee_payroll as p,
orion.Employee_organization as o
where p.Employee_ID=o.Employee_ID
and not Employee_Term_Date
and o.Department="Sales"
group by Job_Title;
quit;
/*Now do inner join:*/
proc sql;
select Last_name, emp.Job_Title,
Salary format=comma7., Job_Avg format=comma7.
from avgsalary as job,
orion.Sales as emp
where emp.Job_Title=job.Job_Title
and Salary < Job_Avg*.95
order by Job_Title, Last_name;
quit;
/*2nd Method*/
proc sql;
title "Sales Department Average Salary";
title2 "By Job Title";
select Job_Title,
avg(Salary) as Job_Avg
format=comma7.
from orion.Employee_payroll as p,
orion.Employee_organization as o
where p.Employee_ID=o.Employee_ID
and not Employee_Term_Date
and o.Department="Sales"
group by Job_Title;
quit;
title;
proc sql;
title "Employees with salaries less than";
title2 "95% of the average for their job";
select Last_Name, emp.Job_Title,
Salary format=comma7., Job_Avg format=comma7.
from (select Job_Title,
avg(Salary) as Job_Avg format=comma7.
from orion.Employee_payroll as p,
orion.Employee_organization as o
where p.Employee_ID=o.Employee_ID
and not Employee_Term_Date
and o.Department="Sales"
group by Job_Title) as job,
orion.Sales as emp
where emp.Job_Title=job.Job_Title
and Salary < Job_Avg*.95
order by Job_Title, Last_Name;
quit;
title;
libname train "&path/train";
proc contents data=train.flightdelays;run;
proc print data=train.flightdelays (obs=100);
run;
proc sql;
title "Flight destinations and delays";
create table delays as
select destination,
avg(delay) as average,
max(delay) as max,
sum(delay>0) as late,
sum(delay<=0) as early
from train.flightdelays
group by destination
order by average;
select destination,
average format=3.0 label="Average Delay",
max format=3.0 label="Maximum Delay",
late/(late+early) as prob format=5.2
label="Probability of Delay"
from delays;
title;
quit;
/*Embed first step in an in-line query*/
proc sql;
title "Flight destinations and delays";
select destination,
average format=3.0 label="Average Delay",
max format=3.0 label="Maximum Delay",
late/(late+early) as prob format=5.2
label="Probability of Delay"
from (select destination,
avg(delay) as average,
max(delay) as max,
sum(delay>0) as late,
sum(delay<=0) as early
from train.flightdelays
group by destination)
order by average;
title;
quit;
/*Step 1*/
proc sql;
select distinct Employee_ID
from orion.Order_Fact as o,
orion.Product_Dim as p
where o.Product_ID=p.Product_ID
and year(Order_Date)=2003
and Product_Name contains
'Expedition Zero'
and Employee_ID ne 99999999;
quit;
/*Step 2*/
proc sql;
select Manager_ID
from orion.Employee_Organization as o,
(select distinct Employee_ID
from orion.Order_Fact as o,
orion.Product_Dim as p
where o.Product_ID=p.Product_ID
and year(Order_Date)=2003
and Product_Name
contains 'Expedition Zero'
and Employee_ID ne 99999999) as ID
where o.Employee_ID=ID.Employee_ID;
quit;
/*Step 3*/
proc sql;
select Employee_Name format=$25. as Name, City
from orion.Employee_Addresses
where Employee_ID in
(select Manager_ID
from orion.Employee_Organization as o,
(select distinct Employee_ID
from orion.Order_Fact as o,
orion.Product_Dim as p
where o.Product_ID=p.Product_ID
and year(Order_Date)=2003
and Product_Name contains
'Expedition Zero'
and Employee_ID ne 99999999) as ID
where o.Employee_ID=ID.Employee_ID);
quit;
/*Multiway Join*/
proc sql;
select distinct Employee_Name format=$25. as Name, City
from orion.Order_Fact as of,
orion.Product_Dim as pd,
orion.Employee_Organization as eo,
orion.Employee_Addresses as ea
where of.Product_ID=pd.Product_ID
and of.Employee_ID=eo.Employee_ID
and ea.Employee_ID=eo.Manager_ID
and Product_Name contains 'Expedition Zero'
and year(Order_Date)=2003
and eo.Employee_ID ne 99999999
;
quit;
/*Query1-Identify Crews for Copenhagen (CPH) flight*/
proc sql;
select empid
from train.flightschedule
where date="04mar2013"d and destination="CPH";
quit;
/*query 2 –get the job categories and states, make query 1 a subquery */
proc sql;
select substr(jobcode,1,2) as JobCategory,state
from train.staffmaster as s,train.payrollmaster as p
where s.empid=p.empid and s.empid in
(select empid
from train.flightschedule
where date="04mar2013"d and destination="CPH");
quit;
/*Find supervisors*/
proc contents data=train.supervisors;run;
proc print data=train.supervisors;run;
/*make query 2 an inline view in query 3*/
proc sql;
select empid
from train.supervisors as m,
(select substr(jobcode,1,2) as JobCategory,state
from train.staffmaster as s,train.payrollmaster as p
where s.empid=p.empid and s.empid in
(select empid
from train.flightschedule
where date="04mar2013"d and destination="CPH")) as c
where m.jobcategory=c.jobcategory
and
m.state=c.state;
quit;
/*make query 3 a subquery to query 4 to get names*/
proc sql;
select firstname,lastname
from train.staffmaster
where empid in
(select empid
from train.supervisors as m,
(select substr(jobcode,1,2) as JobCategory,state
from train.staffmaster as s,train.payrollmaster as p
where s.empid=p.empid and s.empid in
(select empid
from train.flightschedule
where date="04mar2013"d and destination="CPH")) as c
where m.jobcategory=c.jobcategory
and
m.state=c.state);
quit;
/*use traditional sas programming*/
/*find the crew*/
proc sort data=train.flightschedule (drop=flightnumber)
out=crew (keep=empid);
where destination="CPH" and
date="04mar2013"d;
by empid;
run;
/*find the state and job code for crew*/
proc sort data=train.payrollmaster
(keep=empid jobcode) out=payroll;
by empid;
run;
proc sort data=train.staffmaster
(keep=empid state firstname lastname)
out=staff;
by empid;
run;
data st_cat(keep=state jobcategory);
merge crew (in=one)
staff
payroll;
by empid;
if one;
jobcategory=substr(jobcode,1,2);
run;
/* find supervisor id*/
proc sort data=st_cat;
by jobcategory state;
run;
proc sort data=train.supervisors
out=superv;
by jobcategory state;
run;
data super (keep=empid);
merge st_cat(in=s)
superv;
by jobcategory state;
if s;
run;
/*find names of supervisors*/
proc sort data=super;
by empid;
run;
data names (drop=empid);
merge super (in=super)
staff (keep=empid firstname lastname);
by empid;
if super;
run;
proc print data=names noobs uniform;
run;
/*same problem using a multiway join*/
proc sql;
select distinct e.firstname,e.lastname
from train.flightschedule as a,
train.staffmaster as b,
train.payrollmaster as c,
train.supervisors as d,
train.staffmaster as e
where a.date="04mar2013"d and
a.destination="CPH" and
a.empid=b.empid and
a.empid=c.empid and
d.jobcategory=substr(c.jobcode,1,2) and
d.state=b.state and
d.empid=e.empid;
quit;