SAS SQL queries Flashcards
PROC SQL syntax
PROC SQL;
SELECT column1, column2
FROM table1, table2
WHERE expression
GROUP BY column1, column2
ORDER BY column1, column2 DESC;
QUIT;
How do you create a new column in a SELECT clause?
This will create a new column named ‘newcolumn’ that calculates column2*0.5.
proc sql;
select column1, column2, column2*0.5 as newcolumn
from table1
quit;
Do the columns specified in a WHERE clause have to be specified in the SELECT clause?
no
Two ways to write an ORDER BY clause
1 is the column’s position in the SELECT clause
ORDER BY column1
or
ORDER BY 1
How do you join tables?
How do you select a column that appears on both tables?
Remember to set the matching columns in the WHERE clause
proc sql;
select table1.column1, column2, column3
from sasuser.table1, sasuser.table2
where table1.column1 = table2.column1
order by column3;
quit;
When do you use a GROUP BY clause?
If you use a GROUP BY clause in a PROC SQL step without a summary function, the GROUP BY clause is changed to an ORDER BY clause.
When your query contains one or more summary functions and you want to group the results
e.g.
proc sql;
select membertype, sum(milestraveled) as TotalMiles
from sasuser.frequentflyers
group by membertype;
quit;
How do you create a new table from the results of a query?
PROC SQL;
CREATE TABLE table-name AS
SELECT column1, column2
FROM table1, table2
WHERE expression
GROUP BY column1, column2
ORDER BY column1, column2 DESC;
QUIT;
How do you select all columns of a table?
use *
proc sql;
select *
from table1;
quit;
Which option lets you view the list of columns from a SELECT * statement in the SAS log?
This will list out all the columns to the SAS. Also resolves macro variables and places parentheses around expressions to show their order of evaluation.
FEEDBACK option
proc sql feedback;
select *
from table1;
quit;
How do you limit the number of rows displayed in the output?
proc sql outobs= n;
How do you eliminate duplicate rows from the output?
use DISTINCT keyword following SELECT
proc sql;
select distinct column1, column2
from table1;
What happens if you use a column you created in a SELECT clause in a WHERE clause?
How do you fix this?
Note can also rewrite the calculation in the where clause but this is inefficient
ERROR - the WHERE clause is processed before the SELECT clause, so the column isn’t found in the original table
To fix it, use the CALCULATED keyword
proc sql;
select flightnumber, boarded + transferred + nonrevenue as Total
from sasusesr.marchflights
where calculated total < 100;
quit;
How do you make a calculation based on a column you created in the SELECT clause?
Use CALCULATED keywork
proc sql;
select flightnumber, boarded + transferred + nonrevenue as Total, calculated total/2 as Half
from sasusesr.marchflights;
quit;
What 3 things end a PROC SQL step?
QUIT;
Submitting another PROC step or a DATA step