sas sql #1 - wmich.edu · sql—an introduction introduction to proc sql sas sql #1 sas structured...

62
SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU) SAS SQL #1 Lecture 19 1 / 20

Upload: lemien

Post on 15-Jul-2018

249 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

SAS SQL #1SAS Structured Query Language, An Introduction

JC Wang

wang (WMU) SAS SQL #1 Lecture 19 1 / 20

Page 2: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Outline

1 SQL—An IntroductionWhat is SQL

2 Introduction to PROC SQLSAS SQLQuery a TableSorting Query Output

wang (WMU) SAS SQL #1 Lecture 19 2 / 20

Page 3: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

What is SQL Language

Structured Query Language (SQL), a standardized, widely usedlanguage that retrieves / updates data in relational databases.Relation = concept similar to concept of a set in mathematicsRelations are represented physically as two-dimensional tables(arranged in rows and columns).

wang (WMU) SAS SQL #1 Lecture 19 3 / 20

Page 4: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Outline

1 SQL—An IntroductionWhat is SQL

2 Introduction to PROC SQLSAS SQLQuery a TableSorting Query Output

wang (WMU) SAS SQL #1 Lecture 19 4 / 20

Page 5: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

What PROC SQL Does

PROC SQL = SAS implementation of Structured Query Language. Itcan

generate reportsgenerate summary statisticsretrieve data from tables or viewscombine data from tables or viewscreate tables, views, and indexesupdate data values in PROC SQL tablesupdate / retrieve data from database management system(DBMS) tablesmodify PROC SQL table by adding, modifying, or droppingcolumns.

wang (WMU) SAS SQL #1 Lecture 19 5 / 20

Page 6: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

What PROC SQL Does

PROC SQL = SAS implementation of Structured Query Language. Itcan

generate reportsgenerate summary statisticsretrieve data from tables or viewscombine data from tables or viewscreate tables, views, and indexesupdate data values in PROC SQL tablesupdate / retrieve data from database management system(DBMS) tablesmodify PROC SQL table by adding, modifying, or droppingcolumns.

wang (WMU) SAS SQL #1 Lecture 19 5 / 20

Page 7: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

What PROC SQL Does

PROC SQL = SAS implementation of Structured Query Language. Itcan

generate reportsgenerate summary statisticsretrieve data from tables or viewscombine data from tables or viewscreate tables, views, and indexesupdate data values in PROC SQL tablesupdate / retrieve data from database management system(DBMS) tablesmodify PROC SQL table by adding, modifying, or droppingcolumns.

wang (WMU) SAS SQL #1 Lecture 19 5 / 20

Page 8: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

What PROC SQL Does

PROC SQL = SAS implementation of Structured Query Language. Itcan

generate reportsgenerate summary statisticsretrieve data from tables or viewscombine data from tables or viewscreate tables, views, and indexesupdate data values in PROC SQL tablesupdate / retrieve data from database management system(DBMS) tablesmodify PROC SQL table by adding, modifying, or droppingcolumns.

wang (WMU) SAS SQL #1 Lecture 19 5 / 20

Page 9: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

What PROC SQL Does

PROC SQL = SAS implementation of Structured Query Language. Itcan

generate reportsgenerate summary statisticsretrieve data from tables or viewscombine data from tables or viewscreate tables, views, and indexesupdate data values in PROC SQL tablesupdate / retrieve data from database management system(DBMS) tablesmodify PROC SQL table by adding, modifying, or droppingcolumns.

wang (WMU) SAS SQL #1 Lecture 19 5 / 20

Page 10: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

What PROC SQL Does

PROC SQL = SAS implementation of Structured Query Language. Itcan

generate reportsgenerate summary statisticsretrieve data from tables or viewscombine data from tables or viewscreate tables, views, and indexesupdate data values in PROC SQL tablesupdate / retrieve data from database management system(DBMS) tablesmodify PROC SQL table by adding, modifying, or droppingcolumns.

wang (WMU) SAS SQL #1 Lecture 19 5 / 20

Page 11: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

What PROC SQL Does

PROC SQL = SAS implementation of Structured Query Language. Itcan

generate reportsgenerate summary statisticsretrieve data from tables or viewscombine data from tables or viewscreate tables, views, and indexesupdate data values in PROC SQL tablesupdate / retrieve data from database management system(DBMS) tablesmodify PROC SQL table by adding, modifying, or droppingcolumns.

wang (WMU) SAS SQL #1 Lecture 19 5 / 20

Page 12: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

What PROC SQL Does

PROC SQL = SAS implementation of Structured Query Language. Itcan

generate reportsgenerate summary statisticsretrieve data from tables or viewscombine data from tables or viewscreate tables, views, and indexesupdate data values in PROC SQL tablesupdate / retrieve data from database management system(DBMS) tablesmodify PROC SQL table by adding, modifying, or droppingcolumns.

wang (WMU) SAS SQL #1 Lecture 19 5 / 20

Page 13: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

What PROC SQL Does

PROC SQL = SAS implementation of Structured Query Language. Itcan

generate reportsgenerate summary statisticsretrieve data from tables or viewscombine data from tables or viewscreate tables, views, and indexesupdate data values in PROC SQL tablesupdate / retrieve data from database management system(DBMS) tablesmodify PROC SQL table by adding, modifying, or droppingcolumns.

wang (WMU) SAS SQL #1 Lecture 19 5 / 20

Page 14: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Comparison of Terminology

SQL tables = SAS data file = a filerows = observations = recordscolumns = variables = fieldsDBMS tables = tables from other vendors which can be accessedthrough SAS/ACCESSQueries retrieve data from a table, view, or DBMS with resultsconsisting of rows and columns from a table.SQL view contains a stored query (instruction only), i.e., a virtualtableSAS PROC SQL null values = missing values.

wang (WMU) SAS SQL #1 Lecture 19 6 / 20

Page 15: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Comparison of Terminology

SQL tables = SAS data file = a filerows = observations = recordscolumns = variables = fieldsDBMS tables = tables from other vendors which can be accessedthrough SAS/ACCESSQueries retrieve data from a table, view, or DBMS with resultsconsisting of rows and columns from a table.SQL view contains a stored query (instruction only), i.e., a virtualtableSAS PROC SQL null values = missing values.

wang (WMU) SAS SQL #1 Lecture 19 6 / 20

Page 16: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Comparison of Terminology

SQL tables = SAS data file = a filerows = observations = recordscolumns = variables = fieldsDBMS tables = tables from other vendors which can be accessedthrough SAS/ACCESSQueries retrieve data from a table, view, or DBMS with resultsconsisting of rows and columns from a table.SQL view contains a stored query (instruction only), i.e., a virtualtableSAS PROC SQL null values = missing values.

wang (WMU) SAS SQL #1 Lecture 19 6 / 20

Page 17: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Comparison of Terminology

SQL tables = SAS data file = a filerows = observations = recordscolumns = variables = fieldsDBMS tables = tables from other vendors which can be accessedthrough SAS/ACCESSQueries retrieve data from a table, view, or DBMS with resultsconsisting of rows and columns from a table.SQL view contains a stored query (instruction only), i.e., a virtualtableSAS PROC SQL null values = missing values.

wang (WMU) SAS SQL #1 Lecture 19 6 / 20

Page 18: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Syntax of PROC SQLbasic use

PROC SQL <options>;query-1;query-2;...more queries...QUIT;

a query = a run block. Upon submission, SAS executes the query.End PROC SQL with a QUIT since it’s interactive.

wang (WMU) SAS SQL #1 Lecture 19 7 / 20

Page 19: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Syntax of PROC SQLbasic use

PROC SQL <options>;query-1;query-2;...more queries...QUIT;

a query = a run block. Upon submission, SAS executes the query.End PROC SQL with a QUIT since it’s interactive.

wang (WMU) SAS SQL #1 Lecture 19 7 / 20

Page 20: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Syntax of a Queryfrom single table

SELECT input-columnsFROM tableWHERE cond-to-subset-input-rowsGROUP BY columnHAVING cond-to-subset-groupsORDER BY columns;

It’s a single statement in this order of clauses.SELECT and FROM are required.

wang (WMU) SAS SQL #1 Lecture 19 8 / 20

Page 21: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Syntax of a Queryfrom single table

SELECT input-columnsFROM tableWHERE cond-to-subset-input-rowsGROUP BY columnHAVING cond-to-subset-groupsORDER BY columns;

It’s a single statement in this order of clauses.SELECT and FROM are required.

wang (WMU) SAS SQL #1 Lecture 19 8 / 20

Page 22: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

SELECT and FROM Clauses

SELECT input-columnsFROM table;

input-columns: a comma-separated ordered list of columns orthe wild-card ∗ (= all columns in table)table: one-word table (SAS data set) name or two-word namelibref.sas-data-set-name (usually the latter)use SELECT DISTINCT a-table-column to query only uniquevalues of the table column

wang (WMU) SAS SQL #1 Lecture 19 9 / 20

Page 23: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

SELECT and FROM Clauses

SELECT input-columnsFROM table;

input-columns: a comma-separated ordered list of columns orthe wild-card ∗ (= all columns in table)table: one-word table (SAS data set) name or two-word namelibref.sas-data-set-name (usually the latter)use SELECT DISTINCT a-table-column to query only uniquevalues of the table column

wang (WMU) SAS SQL #1 Lecture 19 9 / 20

Page 24: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Input Columns in SELECT Clause

an input column can bea table columna quoted text stringa calculated columna column created by a simple CASE expressiona column created by a CASE-Operand expressiona column created by COALESCE function of columns

wang (WMU) SAS SQL #1 Lecture 19 10 / 20

Page 25: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Input Columns in SELECT Clause

an input column can bea table columna quoted text stringa calculated columna column created by a simple CASE expressiona column created by a CASE-Operand expressiona column created by COALESCE function of columns

wang (WMU) SAS SQL #1 Lecture 19 10 / 20

Page 26: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Input Columns in SELECT Clause

an input column can bea table columna quoted text stringa calculated columna column created by a simple CASE expressiona column created by a CASE-Operand expressiona column created by COALESCE function of columns

wang (WMU) SAS SQL #1 Lecture 19 10 / 20

Page 27: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Input Columns in SELECT Clause

an input column can bea table columna quoted text stringa calculated columna column created by a simple CASE expressiona column created by a CASE-Operand expressiona column created by COALESCE function of columns

wang (WMU) SAS SQL #1 Lecture 19 10 / 20

Page 28: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Input Columns in SELECT Clause

an input column can bea table columna quoted text stringa calculated columna column created by a simple CASE expressiona column created by a CASE-Operand expressiona column created by COALESCE function of columns

wang (WMU) SAS SQL #1 Lecture 19 10 / 20

Page 29: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Input Columns in SELECT Clause

an input column can bea table columna quoted text stringa calculated columna column created by a simple CASE expressiona column created by a CASE-Operand expressiona column created by COALESCE function of columns

wang (WMU) SAS SQL #1 Lecture 19 10 / 20

Page 30: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Input Columns in SELECT Clause

an input column can bea table columna quoted text stringa calculated columna column created by a simple CASE expressiona column created by a CASE-Operand expressiona column created by COALESCE function of columns

wang (WMU) SAS SQL #1 Lecture 19 10 / 20

Page 31: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Dress Up Input Columns with Attributes

input-column <attributes>where attributes are blank-separated column attributes of

FORMAT=

INFORMAT=

LABEL=’text-string’ (or simply ’text-string’)LENGTH=

Use of LABEL=’#’ suppresses the column header.See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 11 / 20

Page 32: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Dress Up Input Columns with Attributes

input-column <attributes>where attributes are blank-separated column attributes of

FORMAT=

INFORMAT=

LABEL=’text-string’ (or simply ’text-string’)LENGTH=

Use of LABEL=’#’ suppresses the column header.See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 11 / 20

Page 33: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Dress Up Input Columns with Attributes

input-column <attributes>where attributes are blank-separated column attributes of

FORMAT=

INFORMAT=

LABEL=’text-string’ (or simply ’text-string’)LENGTH=

Use of LABEL=’#’ suppresses the column header.See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 11 / 20

Page 34: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Dress Up Input Columns with Attributes

input-column <attributes>where attributes are blank-separated column attributes of

FORMAT=

INFORMAT=

LABEL=’text-string’ (or simply ’text-string’)LENGTH=

Use of LABEL=’#’ suppresses the column header.See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 11 / 20

Page 35: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Using Column Alias

input-column AS column-aliaswhere

column-alias is a valid SAS name used as an aliaswhen referencing it in subsequent calculated column(s) in thecalculation, use the alias

See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 12 / 20

Page 36: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Using Column Alias

input-column AS column-aliaswhere

column-alias is a valid SAS name used as an aliaswhen referencing it in subsequent calculated column(s) in thecalculation, use the alias

See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 12 / 20

Page 37: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Using Column Alias

input-column AS column-aliaswhere

column-alias is a valid SAS name used as an aliaswhen referencing it in subsequent calculated column(s) in thecalculation, use the alias

See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 12 / 20

Page 38: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Adding Text to Output

Codesdata test;do r = rank(’A’) to rank(’Z’);c = byte(r);output;

end;run;proc sql;select ’Character’ format=$9.,

r label=’#’ format=2.,’is’, c ’#’

from test;quit;

Output--------------------Character 65 is ACharacter 66 is BCharacter 67 is CCharacter 68 is Dmore output lines here

Character 90 is Z

wang (WMU) SAS SQL #1 Lecture 19 13 / 20

Page 39: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Adding Text to Output

Codesdata test;do r = rank(’A’) to rank(’Z’);c = byte(r);output;

end;run;proc sql;select ’Character’ format=$9.,

r label=’#’ format=2.,’is’, c ’#’

from test;quit;

Output--------------------Character 65 is ACharacter 66 is BCharacter 67 is CCharacter 68 is Dmore output lines here

Character 90 is Z

wang (WMU) SAS SQL #1 Lecture 19 13 / 20

Page 40: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Adding Text to Output

Codesdata test;do r = rank(’A’) to rank(’Z’);c = byte(r);output;

end;run;proc sql;select ’Character’ format=$9.,

r label=’#’ format=2.,’is’, c ’#’

from test;quit;

Output--------------------Character 65 is ACharacter 66 is BCharacter 67 is CCharacter 68 is Dmore output lines here

Character 90 is Z

wang (WMU) SAS SQL #1 Lecture 19 13 / 20

Page 41: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Calculated Column

created byarithmatic-expression <AS alias> <attributes>where alias is required if it is referenced in subsequentcalculated column(s)referencing it byCALCULATED alias

See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 14 / 20

Page 42: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Calculated Column

created byarithmatic-expression <AS alias> <attributes>where alias is required if it is referenced in subsequentcalculated column(s)referencing it byCALCULATED alias

See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 14 / 20

Page 43: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Calculated Column

created byarithmatic-expression <AS alias> <attributes>where alias is required if it is referenced in subsequentcalculated column(s)referencing it byCALCULATED alias

See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 14 / 20

Page 44: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Column Created By CASE Expression

created byCASEWHEN cond-1 THEN expr-1more WHEN-THEN clauses as neededELSE expr-2

END <AS alias> <attributes>

referencing it byCALCULATED alias

See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 15 / 20

Page 45: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Column Created By CASE Expression

created byCASEWHEN cond-1 THEN expr-1more WHEN-THEN clauses as neededELSE expr-2

END <AS alias> <attributes>

referencing it byCALCULATED alias

See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 15 / 20

Page 46: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Column Created By CASE Expression

created byCASEWHEN cond-1 THEN expr-1more WHEN-THEN clauses as neededELSE expr-2

END <AS alias> <attributes>

referencing it byCALCULATED alias

See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 15 / 20

Page 47: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Column Created By CASE-Operand Expression

created byCASE operandWHEN value-1 THEN expr-1more WHEN-THEN clauses as neededELSE expr-2

END <AS alias> <attributes>

referencing it byCALCULATED alias

See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 16 / 20

Page 48: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Column Created By CASE-Operand Expression

created byCASE operandWHEN value-1 THEN expr-1more WHEN-THEN clauses as neededELSE expr-2

END <AS alias> <attributes>

referencing it byCALCULATED alias

See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 16 / 20

Page 49: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Column Created By CASE-Operand Expression

created byCASE operandWHEN value-1 THEN expr-1more WHEN-THEN clauses as neededELSE expr-2

END <AS alias> <attributes>

referencing it byCALCULATED alias

See example sql01.sas.

wang (WMU) SAS SQL #1 Lecture 19 16 / 20

Page 50: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Column Created By COALESCE FunctionUsage:COALESCE(col-1,col-2<, ...>)

<AS alias> <attributes>returns first non-missing value of the columns; returns missing value if allmissing. For example,

select Name,coalesce(LowPoint, ’*Not Available*’) as LowPoint

from mylib.continents;select Name,

casewhen LowPoint is missing then ’*Not Available*’else LowPoint

end as LowPointfrom mylib.continents;

wang (WMU) SAS SQL #1 Lecture 19 17 / 20

Page 51: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Subset Input Table Using WHERE ClauseSELECT input-columns

FROM tableWHERE cond;

where cond is expression that makes use oftable columns and/or input columns and with comparisonoperators (symbols or mnemonics), logical operators (symbols ormnemonics)special operators including BETWEEN-AND, CONTAINS, IN, ISNULL/IS MISSING, LIKE, and =∗truncated string comparison operators (3-letter mnemonicsformed by appending 2-letter comparison operators)special operators linking subquery: ANY (subquery), ALL(subquery), and EXISTS (subquery)

wang (WMU) SAS SQL #1 Lecture 19 18 / 20

Page 52: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Subset Input Table Using WHERE ClauseSELECT input-columns

FROM tableWHERE cond;

where cond is expression that makes use oftable columns and/or input columns and with comparisonoperators (symbols or mnemonics), logical operators (symbols ormnemonics)special operators including BETWEEN-AND, CONTAINS, IN, ISNULL/IS MISSING, LIKE, and =∗truncated string comparison operators (3-letter mnemonicsformed by appending 2-letter comparison operators)special operators linking subquery: ANY (subquery), ALL(subquery), and EXISTS (subquery)

wang (WMU) SAS SQL #1 Lecture 19 18 / 20

Page 53: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Subset Input Table Using WHERE ClauseSELECT input-columns

FROM tableWHERE cond;

where cond is expression that makes use oftable columns and/or input columns and with comparisonoperators (symbols or mnemonics), logical operators (symbols ormnemonics)special operators including BETWEEN-AND, CONTAINS, IN, ISNULL/IS MISSING, LIKE, and =∗truncated string comparison operators (3-letter mnemonicsformed by appending 2-letter comparison operators)special operators linking subquery: ANY (subquery), ALL(subquery), and EXISTS (subquery)

wang (WMU) SAS SQL #1 Lecture 19 18 / 20

Page 54: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Subset Input Table Using WHERE ClauseSELECT input-columns

FROM tableWHERE cond;

where cond is expression that makes use oftable columns and/or input columns and with comparisonoperators (symbols or mnemonics), logical operators (symbols ormnemonics)special operators including BETWEEN-AND, CONTAINS, IN, ISNULL/IS MISSING, LIKE, and =∗truncated string comparison operators (3-letter mnemonicsformed by appending 2-letter comparison operators)special operators linking subquery: ANY (subquery), ALL(subquery), and EXISTS (subquery)

wang (WMU) SAS SQL #1 Lecture 19 18 / 20

Page 55: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Subset Input Table Using WHERE ClauseSELECT input-columns

FROM tableWHERE cond;

where cond is expression that makes use oftable columns and/or input columns and with comparisonoperators (symbols or mnemonics), logical operators (symbols ormnemonics)special operators including BETWEEN-AND, CONTAINS, IN, ISNULL/IS MISSING, LIKE, and =∗truncated string comparison operators (3-letter mnemonicsformed by appending 2-letter comparison operators)special operators linking subquery: ANY (subquery), ALL(subquery), and EXISTS (subquery)

wang (WMU) SAS SQL #1 Lecture 19 18 / 20

Page 56: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Summarizing Datausing aggregate functions

Aggregate functions (or summary functions) are used to combinecolumns. If single column is specified then the column statistic isproduced; otherwise, row statistics are produced for the columns.Aggregate functions include

1 AVG/MEAN, MIN, MAX, SUM SUMWGT2 RANGE, STD, STDERR, CV3 USS, CSS, VAR4 COUNT, FREQ, N, NMISS5 T, PRT

wang (WMU) SAS SQL #1 Lecture 19 19 / 20

Page 57: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Summarizing Datausing aggregate functions

Aggregate functions (or summary functions) are used to combinecolumns. If single column is specified then the column statistic isproduced; otherwise, row statistics are produced for the columns.Aggregate functions include

1 AVG/MEAN, MIN, MAX, SUM SUMWGT2 RANGE, STD, STDERR, CV3 USS, CSS, VAR4 COUNT, FREQ, N, NMISS5 T, PRT

wang (WMU) SAS SQL #1 Lecture 19 19 / 20

Page 58: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Sorting Query Results

SELECT input-columnFROM tableORDER BY sort-columns;

where columns is a comma-separated list of columns and eachsort-column

can be an input-column either by name or by its position (anumber) in the input-column listcan be a table column but not an input columnif it’s a calculated column, do NOT use CALCULATED keyworduse sort-column DESC for a sort-column to be in descendingorder

wang (WMU) SAS SQL #1 Lecture 19 20 / 20

Page 59: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Sorting Query Results

SELECT input-columnFROM tableORDER BY sort-columns;

where columns is a comma-separated list of columns and eachsort-column

can be an input-column either by name or by its position (anumber) in the input-column listcan be a table column but not an input columnif it’s a calculated column, do NOT use CALCULATED keyworduse sort-column DESC for a sort-column to be in descendingorder

wang (WMU) SAS SQL #1 Lecture 19 20 / 20

Page 60: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Sorting Query Results

SELECT input-columnFROM tableORDER BY sort-columns;

where columns is a comma-separated list of columns and eachsort-column

can be an input-column either by name or by its position (anumber) in the input-column listcan be a table column but not an input columnif it’s a calculated column, do NOT use CALCULATED keyworduse sort-column DESC for a sort-column to be in descendingorder

wang (WMU) SAS SQL #1 Lecture 19 20 / 20

Page 61: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Sorting Query Results

SELECT input-columnFROM tableORDER BY sort-columns;

where columns is a comma-separated list of columns and eachsort-column

can be an input-column either by name or by its position (anumber) in the input-column listcan be a table column but not an input columnif it’s a calculated column, do NOT use CALCULATED keyworduse sort-column DESC for a sort-column to be in descendingorder

wang (WMU) SAS SQL #1 Lecture 19 20 / 20

Page 62: SAS SQL #1 - wmich.edu · SQL—An Introduction Introduction to PROC SQL SAS SQL #1 SAS Structured Query Language, An Introduction JC Wang wang (WMU)SAS SQL #1 Lecture 19 1 / 20

SQL—An Introduction Introduction to PROC SQL

Sorting Query Results

SELECT input-columnFROM tableORDER BY sort-columns;

where columns is a comma-separated list of columns and eachsort-column

can be an input-column either by name or by its position (anumber) in the input-column listcan be a table column but not an input columnif it’s a calculated column, do NOT use CALCULATED keyworduse sort-column DESC for a sort-column to be in descendingorder

wang (WMU) SAS SQL #1 Lecture 19 20 / 20