Proc SQL Flashcards

Get familiar with SQL part of the exam

1
Q

The following datastep executed to create the dataset suntimes containing sunrise and sunset times.

data suntimes;
merge sunrise(in=a) sunset(in=b)
by date ;
run;

How would you write this in SQL?

A

This would be a full join in SQL terms.
We would take all records from set A and B and observations inside the intersection of C.

Under the assumption there are no missing observations in the key variable (in this case date) we can write this step as follows:

proc sql;
   create table suntimes as
      select * 
      from sunrise a
      full join sunset b 
      on a.date=b.date ;
quit ;

If the key variable does however contain missing values, the coalesce function should be used and the step should be written in the following manner.

proc sql;
   create table suntimes as
      select coalesce(a.date, b.date) as date, risetime, settime
      from sunrise a
      full join sunset b 
      on a.date=b.date ;
quit ;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give an example of proc sql step with a having statement that contains a non-corolated subquery.

A
Model answer
proc sql;    
   select jobcode, avg(salary) as AvgSalary format=dollar11.2  
   from sasuser.payrollmaster       
   group by jobcode      
   having avg(salary) >
       (select avg(salary) 
        from sasuser.payrollmaster);
quit ;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

If there are multiple records in a subquery, what operator can you use

A

The in operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What type of join is the following statement

proc sql ;
   select * 
   from a, b 
   where a.x = b.x ;
quit ;
A

An Inner Join

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe what a full outer join does

A

Matching and non-macthing rows in dataset a and dataset b. i.e. a&b a b.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does a left outer join work

A

Returns all data from dataset a and matching rows of datatset b i.e. a&b a.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does a right outer join work

A

Returns all data from dataset b and matching rows of datatset a i.e. a&b b.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

data merged ;
merge a b ;
by x;
run ;

What type of merge is this and what rows will it contain, given that all values in dataset a have a matching value of x for dataset b,

A

An inner join, this will contain all data in the intersect of a and b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

data merged ;
merge a b ;
by x;
run ;

What type of merge is this and what rows will it contain, given that NOT all values in dataset a have a matching value of x for dataset b,

A

An full outer join and will contain all rows non-matching rows in dataset a, dataset b and matching rows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When should you use the coalesce during an SQL join

A

When you want an outer join to produce the same results as a data-step match-merge

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write the equivalent proc sql statement for this match merge.

data ten ;
merge seven eight ;
by nine ;
run ;

A
proc sql ;
   create table ten as
   select coalesce(a.nine,b.nine) as nine, var1,  var2
   from seven a
   full join eight b 
   on a.nine=b.nine ;
quit ;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why might you use proc sql joins

Give three reasons

A

Tables do not need to be indexed or sorted
names can be different for columns
Other comparison operators can be used and not just the equal sign

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an in-line view?

A

A nested query specified in the outer query’s from clause. It exists only during the execution of the step.

e.g.
from (select bread_type, filling
from sasuser.sandwiches)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Will the columns be overlaid in this SQL statment if there are more fields in filling than sandwich?

proc sql  ;
   select * from sandwich a
   inner join filling b
   on a.id=b.id ;
quit ;
A

No because both sandwich and filling contain a separate ID fields. Also only fields in the intersection will be reported only.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does the corr or corresponding function do on intersect outer

A

Removes columns not present in both tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly