Basic Queries Specifying Columns

Slides:



Advertisements
Similar presentations
7 Copyright © Oracle Corporation, All rights reserved. Producing Readable Output with i SQL*Plus.
Advertisements

11 Chapter 3: Displaying Query Results 3.1 Presenting Data 3.2 Summarizing Data.
This course has taken from This unique introductory SQL tutorial not only provides easy-to-understand SQL instructions, but it allows.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Introduction to SQL Session 1 Retrieving Data From a Single Table.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
11 Chapter 2: Basic Queries 2.1: Overview of the SQL Procedure 2.2: Specifying Columns 2.3: Specifying Rows.
Oracle 11g DATABASE DEVELOPMENT LAB1. Introduction  Oracle 11g Database:-  Oracle 11g database is designed for some features, which helps to the organizations.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Chapter 14 Formatting Readable Output. Chapter Objectives  Add a column heading with a line break to a report  Format the appearance of numeric data.
SAS for Data Management and Analysis
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
11 Chapter 4: Subqueries 4.1: Noncorrelated Subqueries 4.2: Correlated Subqueries (Self-Study)
1 Checking Data with the PRINT and FREQ Procedures.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
1 Cleaning Invalid Data. Clean data by using assignment statements in the DATA step. Clean data by using IF-THEN / ELSE statements in the DATA step. 2.
Copyright 2009 The Little Engine That Could: Using EXCEL LIBNAME Engine Options to Enhance Data Transfers between SAS® and Microsoft® Excel Files William.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 5 & 6 By Ravi Mandal.
1 Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 3 & 4 By Tasha Chapman, Oregon Health Authority.
Numbers in ‘C’ Two general categories: Integers Floats
Session 1 Retrieving Data From a Single Table
Restricting and Sorting Data
Retrieving Data Using the SQL SELECT Statement
Chapter 6: Set Operators
Writing Basic SQL SELECT Statements
Displaying Query Results
Basic select statement
Two “identical” programs
PROC SQL, Overview.
Chapter 5: Using DATA Step Arrays
An Introduction to SQL.
Writing Basic SQL SELECT Statements
Conditional Processing
Tamara Arenovich Tony Panzarella
Noncorrelated subquery
Correlated Subqueries
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
A more complex example.
Displaying Queries 2 Display a query’s results in a specified order.
Subsetting Rows with the WHERE clause
Outer Joins Inner joins returned only matching rows. When you join tables, you might want to include nonmatching rows as well as matching rows.
Grouping Summary Results
Inner Joins.
Writing Basic SQL SELECT Statements
Restricting and Sorting Data
5 The EXCEPT Operator Unique rows from the first result set that are not found in the second result set are selected.
The INTERSECT Operator
Lab 2 HRP223 – 2010 October 18, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected.
Creating Tables Create a new table by defining the column structure.
WEEK-2.
3 Views.
Shortcuts for Variable Lists in SAS
A new keyword -- calculated
Subqueries.
Data Manipulation (with SQL)
UNION Operator keywords Displays all rows from both the tables
Remerging Summary Values
Producing Readable Output with iSQL*Plus
Restricting and Sorting Data
Presentation transcript:

Basic Queries Specifying Columns 3 Basic Queries Specifying Columns

Produce a report that contains the employee identifier, gender, and salary for all Orion Star employees. The data is contained in the orion.Employee_Payroll table

Retrieving Descripter Data from a Table proc sql; describe table orion.Employee_Payroll ; quit; s102d07

Retrieving Data from a Table (* means everything) proc sql; select * from orion.Employee_Payroll ; quit;

The FEEDBACK Option expands the * (in the log) PROC SQL FEEDBACK; SELECT * FROM table-1|view-1<, ...table-n|view-n> <WHERE expression> <GROUP BY column-1<, …column-n>> <HAVING expression> <ORDER BY column-1<DESC><, …column-n>>; QUIT;

proc sql feedback; select * from orion.Employee_Payroll; quit;

Retrieving Data from a Table proc sql; select Employee_ID, Employee_Gender,Salary from orion.Employee_Payroll; quit; Desired variable are listed following the select keyword Column names are separated by commas No comma after the last variable name

Creating New Columns

Creating New Columns Create a new column by including any valid SAS expression in the select clause Assign a column alias or a name to the new column by using keyword AS The new column can be either text or results of a calculation (new columns exist only for the duration of the query unless a table or view is created).

proc contents data=orion.Employee_Payroll position;run; Display employee_id, salary, and add a new column named Bonus containing 10% of the employee’s salary.

Calculated Columns proc sql; select Employee_ID, Salary, Salary * .10 as Bonus from orion.Employee_Payroll; quit; Note that the definition of bonus is backwards from what we would do in the data step. We do the calculation first, then the as keyword. The function of the as keyword is just to put in the column header of the report. When a data set is created, the as keyword causes a variable of that name to be created.

Modify the bonus report to conditionally calculate bonuses based on the employee’s job title: Level I employees receive a 5% bonus. Level II employees receive a 7% bonus. Level III employees receive a 10% bonus. Level IV employees receive a 12% bonus. All others receive an 8% bonus.

proc contents data=orion.staff position;run; The Staff table contains the information needed to create this report.

proc freq data=orion.staff nlevels; tables job_title/noprint; run;

from orion.staff(keep=job_title salary obs=10); quit; proc sql; select * from orion.staff(keep=job_title salary obs=10); quit; Level I employees receive a 5% bonus. Level II employees receive a 7% bonus. Level III employees receive a 10% bonus. Level IV employees receive a 12% bonus. All others receive an 8% bonus. Employees who have a “level” have a job title ending with the roman numeral specifying the level, those without the roman numeral get an 8% bonuse

The SCAN Function (Review) The SCAN function returns the nth word or segment from a character string after breaking it up by the delimiters. General form of the SCAN function: string a character constant, variable, or expression n an integer specifying the number of the word or segment that you want SCAN to select charlist characters used as delimiters to separate words modifier a character that modifies the action of the SCAN function SCAN(string,n<,charlist><,modifier(s)>)

Extracting the Level from Job_Title Return the third word from Job_Title and use a blank space as the delimiter. scan(Job_Title,3,' ') Office Assistant II II 1 2 3 ...

Extracting the Level from Job_Title scan(Job_Title,3,' ') Secretary I 1 2 If Job_Title values have fewer than three words. If the value of n is greater than the number of words in the character string, the SCAN function returns a missing value.

Extracting the Level from Job_Title If the value of n is negative, the SCAN function selects the word in the character string starting from the end of the string. scan(Job_Title,-1,' ') Secretary I I -2 -1 Office Assistant II II -3 -2 -1

The CASE Expression You can use a CASE expression in a SELECT statement to create new columns. General form of the CASE expression in the SELECT statement: SELECT column-1<, ...column-n> CASE <case-operand> WHEN when-condition THEN result-expression <WHEN when-condition THEN result-expression> <ELSE result-expression> END <as column> FROM table;

Calculating the Bonus, Method 1 proc sql; select Job_Title, Salary, case scan(Job_Title,-1,' ') when 'I' then Salary*.05 when 'II' then Salary*.07 when 'III' then Salary*.10 when 'IV' then Salary*.12 else Salary*.08 end as Bonus from orion.Staff ; quit;

Calculating the Bonus, Method 2 proc sql; select Job_Title, Salary, case when scan(Job_Title,-1,' ')='I' then Salary*.05 when scan(Job_Title,-1,' ')='II' then Salary*.07 when scan(Job_Title,-1,' ')='III' then Salary*.10 when scan(Job_Title,-1,' ')='IV' then Salary*.12 else Salary*.08 end as Bonus from orion.Staff ; quit; s102d09a

SAS Dates and Date Functions in PROC SQL

Prepare a report including employee identifier, gender and age proc contents data=orion.Employee_Payroll position;run; Need to calculate age based on birth date and date calculation is made

SAS Date Values (Review) A SAS date is stored as the number of whole days between January 1, 1960, and the date specified. Stored Values -365 366 Display Values (formatted MMDDYY10.) 01/01/1959 01/01/1960 01/01/1961

Some SAS Numeric Functions frequently used when you work with SAS dates Used To Return Example TODAY() today’s date in SAS date form today() as date MONTH(arg) the month portion of a SAS date variable as an integer between 1-12 month(Birth_Date) as Birth_Month INT(arg) the integer portion of a numeric value int(fullage) as age

Calculated Columns Using SAS Dates -- Calculating the age of each employee. proc sql; select Employee_ID, Employee_Gender, int((today()-Birth_Date)/365.25) as Age from orion.Employee_Payroll; quit; s102d10 ...

Using SAS Dates in Calculations Calculate Age based on today’s date being 14NOV2007 and a Birth_Date value of 18AUG1976. 6074 17484 proc sql; select Employee_ID, Employee_Gender, int((today()-Birth_Date)/365.25) as Age from orion.Employee_Payroll; quit; s102d10 ...

Using SAS Dates in Calculations Calculate Age based on today’s date being 14NOV2007 and a Birth_Date value of 18AUG1976. proc sql; select Employee_ID, Employee_Gender, int((today()-Birth_Date)/365.25) as Age from orion.Employee_Payroll; quit; s102d10 31.23887748 ...

Using SAS Dates in Calculations Calculate Age based on today’s date being 14NOV2007 and a Birth_Date value of 18AUG1976. proc sql; select Employee_ID, Employee_Gender, int((today()-Birth_Date)/365.25) as Age from orion.Employee_Payroll; quit; 31 s102d10 31.23887748 ...

Using SAS Dates in Calculations Calculate Age based on today’s date being 14NOV2007 and a Birth_Date value of 18AUG1976. proc sql; select Employee_ID, Employee_Gender, int((today()-Birth_Date)/365.25) as Age from orion.Employee_Payroll; quit; s102d10 31

Creating Tables with PROC SQL

Create a table (file, data set) CREATE TABLE table-name AS query-expression;

Create and Populate a Table with an SQL Query proc sql; create table work.birth_months as select Employee_ID, Birth_Date, month(Birth_Date) as Birth_Month, Employee_gender from orion.Employee_Payroll ; describe table work.birth_months quit; proc print data=birth_months; run;

A review of variable list shortcuts in SAS

Shortcuts for Variable Lists in SAS (Review)

Refer to all variable with the same prefix /* all body measurements begin with "bm"*/ data body; set nhanes3.exam (keep=seqn bm:); run; proc contents data=body;

Refer to Numbered variables libname fram "&path/fram"; /*use single - for numbered variables*/ proc contents data=fram.fram40; run; data sbp20; set fram.fram40(keep=id spf1-spf20); proc print data=sbp20 (obs=5);

Sometimes the order of variables on a file is determines their “grouping,” proc contents data=fram.fram40 position; run;

Refer to contiguous variables Using -- /* double dash -- signifies all contiguous variables from the first to last specified */ data lipids; set fram.fram40(keep=ex_date--vldl); run; proc contents data=lipids; proc print data=lipids (obs=10);

Rename data set option data sbp20; set fram.fram40(keep=id spf1-spf20 rename=(spf1-spf20=sbp1-sbp20)); run; proc print data=sbp20 (obs=5);

Mix and Match data lipids; set fram.fram40(keep=ex_date--vldl spf1-spf20 sex chd rename=(spf1-spf20=sbp1-sbp20)); run; proc contents data=lipids; proc print data=lipids (obs=10);

Multiple Drop Options data chdmen (drop=male); set s5238.chd5238(drop=dead eversmok height smkamt weight); where male; run; proc contents data=chdmen;

A problem with queries is that you can’t use SAS variable lists to specify variables in a select statement. So, use data set options to accomplish pre-processing.

Using data set options – a SAS enhancement proc sql; select libname,memname,nvar from dictionary.tables where memname="EXAM" and libname="NHANES3" ; quit; A limitation in sql is the lack of variable lists, you can get around much of this limitation with data set options.

proc sql; create table body as select * from nhanes3.exam (keep=seqn bm:) order by seqn ; quit; proc means data=body; run;

proc sql; create table sbp as select spf1-spf20 from fram.fram40 ; quit; proc print data=sbp; run;

proc sql; create table sbp as select * from fram.fram40 (obs=10 keep=spf1-spf20 rename=(spf1-spf20=sbp1-sbp20)) ; quit; proc print data=sbp; run;