480 likes | 633 Views
PROC SQL – Select Codes To Master For Power Programming Codes and Examples from SAS.com. Nethra Sambamoorthi, PhD Northwestern University Master of Science in Predictive Analytics Program . Data Processing Terminologies Across Data Sciences…. Why PROC SQL or What Can It Do For Analysts?.
E N D
PROC SQL – Select Codes To Master For Power Programming Codes and Examples from SAS.com Nethra Sambamoorthi, PhD Northwestern University Master of Science in Predictive Analytics Program
Why PROC SQL or What Can It Do For Analysts? • Generate reports • Generate summary statistics • Retrieve data from tables or views • Combine data from tables or views • Create tables, views, and indexes • Update the data values in PROC SQL tables • Update and retrieve data from database management system (DBMS) tables • Modify a PROC SQL table by adding, modifying, or dropping columns • PROC SQL can be used in an interactive SAS session or within batch programs, and it • Can include global statements, such as TITLE and OPTIONS.
An Example of Extracting, Summarizing, and Printing Using Data Step title 'Large Countries Grouped by Continent'; proc summary data=sql.countries; where Population > 1000000; class Continent; var Population; output out=sumPopsum=TotPop; run; procsort data=SumPop; by totPop; run; procprint data=SumPopnoobs; var Continent TotPop; format TotPop comma15.; where _type_=1; run; /* Extracting and summarizing */ /* Sorting to arrange the output */ /* Printing */
Creating The Same Using PROC SQL procsql; title 'Population of Large Countries Grouped by Continent'; select Continent, sum(Population) as TotPop format=comma15. from sql.countries where Population gt 1000000 group by Continent order by TotPop; quit;
Three Important Aspects – Describe, Print, Quit /* Helps understand the structure of the table */ PROC SQL; Describe table sql.unitedstates; Quit;
SELECT means PRINTING is Included Unless • SELECT * /* all columns */ • SELECT city, state /* specific columns */ • SELECT distinct continent /* specific columns but avoid dup */ So it is possible to run this
Retrieving Data From Multiple Tables • Means we are JOINING tables • If there is no JOIN statement, it means (1) Cartesian product of records [no subset condition ] or (2) inner joins [ we need some subset condition] • Alias names can be used for tables too; it helps simplify calling specific columns of a table
Order the output from INNER JOIN INNER JOIN can be used explicitly
Columns are directly comparable between two tables… Capitals FROM sql.unitedstates City FROM sql.uscitycoord Postalcodes FROM sql.postalcodes
NATURAL is applicable for both LEFT and RIGHT JOIN. The purpose is to reduce verbose to match on multiple common columns… Gives the same output; Non matching rows have missing values
Use COALESCE to combine multiple columns to create new matching variables
UNION is ROWWISE (PROC APPEND), while JOIN is COLUMNWISE (MERGE by) Keep the dups