Creating and Managing Views Using PROC SQL in SAS

chapter 7 n.w
1 / 18
Embed
Share

"Learn how to create and manage views in SAS using PROC SQL. Understand the benefits of views, like saving space, ensuring data accuracy, and maintaining data confidentiality. Explore examples of creating inner and outer views for efficient data processing."

  • SAS
  • PROC SQL
  • Views
  • Data Management
  • SAS Programming

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Chapter 7 Creating and Managing Views Using PROC SQL 1

  2. What is a View? A view is a stored query that is executed when the view is used. The view does not contain the data, just the logic for accessing the data. Views can be used in a SAS procedure, data step, or function. Views can be joined with tables or other views. 2

  3. Why Are Views Useful Often save space Ensure input data sets are always current, because data is derived from tables at execution time Hide confidential columns (e.g., SSN) while allowing access to view other columns in the same table 3

  4. Computer Exercise 1 SSN 123-11-5526 206-67-1454 323-50-2746 367-47-9580 564-94-6508 782-57-8284 354-83-9603 590-71-6170 060-76-2046 983-13-6455 638-03-0350 861-59-4706 Major Code 135 135 126 126 126 135 126 135 126 135 135 126 Degree Bachelors Bachelors Masters Bachelors Bachelors Masters Masters Bachelors Bachelors Masters Bachelors Bachelors Grade 9 10 9 9 9 10 10 8 9 9 9 9 4

  5. Homework 1 SSN 367-47-9580 564-94-6508 060-76-2047 123-11-5526 206-67-1454 638-03-0350 861-59-4706 983-13-6455 782-57-8284 354-83-9603 590-71-6170 Major Code 126 126 126 135 135 135 126 135 135 126 135 Degree Bachelors Bachelors Bachelors Bachelors Bachelors Bachelors Bachelors Masters Masters Masters Bachelors HW 1 19 19 17 20 19 19 20 19 19 19 18 5

  6. Creating a View proc sql; create view viewname as select column1, column2, , columnn from table1 <where expression> <group by column1, ,columnn> <having expression> <order by column1, ,columnn>; *The where, group by, having, and order by clauses are optional. 6

  7. Creating a View Inner view: Inner view: select ce1.code label= Major Code ,ce1.degree label= Degree ,ce1.grade as ce1 label= Computer Exercise 1 , hw1.grade as hw1 label= Homework 1 from ce1 left join hw1 on ce1.ssn=hw1.ssn 7

  8. Creating a View Outer clause: Outer clause: proc sql; select code,avg(ce1) as ceavg label= CE 1 format=4.1, avg(hw1) as hw1avg label= HW 1 format=5.1 from . . . group by code; quit; 8

  9. Creating a View Create and execute the view: Create and execute the view: proc sql; create view Major_Comp as . . .; quit; proc sql; select * Major_Comp; quit; 9

  10. Using a View Views can also be used in PROC steps: Views can also be used in PROC steps: proc sql; create view JoinGrades as select ce1.code label= Major Code ,ce1.degree label= Degree ,ce1.grade as ce1 label= Computer Exercise 1 , hw1.grade as hw1 label= Homework 1 from ce1 left join hw1 on ce1.ssn=hw1.ssn order by code;quit; 10

  11. Using a View Views can also be used in PROC steps: Views can also be used in PROC steps: proc means data=JoinGrades maxdec=2; by code; var ce1 hw1; run; 11

  12. Describe View Statement Use a DESCRIBE VIEW statement to display the definition of a view in the SAS log. proc sql; describe view Major_Comp JoinGrades; quit; 12

  13. Guidelines for Using Views Avoid using the ORDER BY clause in a view definition, otherwise the data will have to be sorted each time the view is executed. It is more efficient to create a table if the same data is used many times in one program 13

  14. Guidelines for Using Views Avoid creating views that are based on tables whose structure (e.g., columns in table) may change Specify a one level name (e.g., claims, not work.claims) in the FROM clause if a view resides in the same SAS library as the contributing table(s) 14

  15. USING LIBNAME References to a single-level table name assume the table is in the same library as the view USING LIBNAME can be appended to the CREATE VIEW clause to resolve confusion in table references 15

  16. USING LIBNAME proc sql; create view viewname as select * from libname.table1 using libname directory ; 16

  17. Updating a View Underlying tables can be updated with UPDATE, INSERT, and DELETE Limitations Only a single table can be updated Views with WHERE clauses can be updated Views with ORDER BY, HAVING or GROUP BY cannot be updated - - - 17

  18. Dropping a View To drop (delete) a view, use the drop view statement. proc sql; drop view viewname; quit; 18

More Related Content