• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
SAS Proc SQL
 

SAS Proc SQL

on

  • 16,621 views

Learning ...

Learning
Base SAS,
Advanced SAS,
Proc SQl,
ODS,
SAS in financial industry,
Clinical trials,
SAS Macros,
SAS BI,
SAS on Unix,
SAS on Mainframe,
SAS interview Questions and Answers,
SAS Tips and Techniques,
SAS Resources,
SAS Certification questions...

visit http://sastechies.blogspot.com

Statistics

Views

Total Views
16,621
Views on SlideShare
16,565
Embed Views
56

Actions

Likes
21
Downloads
0
Comments
1

4 Embeds 56

http://www.slideshare.net 52
http://webcache.googleusercontent.com 2
http://www.search-results.com 1
https://compass2g.illinois.edu 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • PROC SQL presentation
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005
  • SASTechies.com Sharad C Narnindi - Attic Technologies,Inc 2005

SAS Proc SQL SAS Proc SQL Presentation Transcript

  • SAS Techies [email_address] http://www.sastechies.com
    • Creating
      • SAS Tables,
      • Listings,
      • Basic Statistics Procedures with SAS
      • Graphs
      • and ODS HTML
      • Proc Report
    • Advanced SAS Programming Concepts
      • SAS Macros
      • SQL Joins
      • Match merging
      • Arrays for Look up
    TLG’s 11/13/09 SAS Techies 2009
    • You may want to query your data to
      • examine relationships between data values
      • view a subset of your data
      • compute values quickly.
    11/13/09 SAS Techies 2009
    • PROC SQL  is SAS software's implementation of Structured Query Language, or SQL, a standardized language that is widely used to retrieve and update data in tables and views based on those tables.
    11/13/09 SAS Techies 2009 Data Processing SAS SQL       File     SAS Data Set       Table       Record       Observation       Row       Field     Variable       Column
    • Any source
    • You can use PROC SQL to
      • retrieve and manipulate SAS tables
      • add or modify data values in a table
      • add, modify, or drop columns in a table
      • create tables
      • generate reports.
    • Advantage of PROC SQL over Merge Statement –
      • the tables do not need a common variable.
    11/13/09 SAS Techies 2009
    • Unlike other PROC steps, PROC SQL is made up of statements and clauses .
    • One single statement only…
    • RUN statement has no effect on the execution
    • Quit statement is required to stop execution
    proc sql; select id,lastname,netpay,grosspay, grosspay*.06 as bonus from emplib.payroll where netpay>25000 order by lastname; Run; Select * from table; Quit; 11/13/09 SAS Techies 2009 ID LastName NetPay GrossPay bonus 1002 BOWMAN $29,048.50 $42,120.33 2527.22 1007 BROWN $37,049.40 $53,927.72 3235.663 1049 FERNANDEZ $25,169.63 $35,956.61 2157.397 1006 GARRETT $34,013.88 $47,241.50 2834.49 1077 GIBSON $41,553.94 $61,108.73 3666.524 1008 HERNAND $54,189.70 $78,575.07 4714.504
      • You use a SELECT clause to specify the columns to be selected.
      • Use the keyword AS to create a new column that will be assigned the value of the preceding column or expression.
    proc sql feedback ; select id, lastname, netpay, grosspay, “ JOHN” as Faculty, grosspay*.06 as bonus from emplib.payroll where netpay>25000 and Calculated bonus > 25250 order by lastname; 11/13/09 SAS Techies 2009
    • you then specify the table to be queried in the FROM clause.
    • Alias statement should be the last if used.
    • Same table can be used with different Aliases.
    proc sql; select id, grosspay*.06 as bonus from emplib.payroll ; proc sql; select id, grosspay*.06 as bonus from emplib.payroll A, emplib.contacts B Where A.id=B.ident; proc sql; select id, grosspay*.06 as bonus from emplib.payroll (where=(grosspay<2000)) A; 11/13/09 SAS Techies 2009
    • you use a WHERE clause in the SELECT statement to subset data according to the criteria
    • Use the where= and other dataset option wisely to bring in data and further filter.
    proc sql; select id, grosspay*.06 as bonus from emplib.payroll where netpay>25000 and Calculated bonus<30000 and grosspay<2000 order by lastname; proc sql; select id, grosspay*.06 as bonus from emplib.payroll (where=(grosspay<2000)) A where netpay>25000 and Calculated bonus<30000 order by lastname; 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
    • Proc sort data=some;
    • By var1 var2;
    • By descending var1 descending var2
    • Run;
      • you can use the ORDER BY clause in the SELECT statement to sort rows by the values of specific columns
      • Specify the keywords ORDER BY, followed by one or more column names separated by commas.
      • Same rules of the by variable in the Proc Sort apply to Proc SQL
    proc sql; select id, lastname, grosspay*.06 as bonus from emplib.payroll where netpay>25000 and Calculated bonus > 25250 order by lastname , firstname; order by 2 ; order by jobcode desc ; 11/13/09 SAS Techies 2009
    • You can use Summary functions in Select, Where and Having clauses only.
    • You can use the GROUP BY clause to group observations and apply a summary function to the group.
    • When you use a GROUP BY clause, you can also use a HAVING clause to specify the condition(s) that each group must satisfy in order to be included in the query output.
    • If you use Having by with NO Group by clause SAS interprets it as a WHERE Clause
    proc sql; select custname as name, count(*) as count from sql.customer group by name * keyword calculated having count(*)=1 ; not required 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
    • proc sql feedback outobs= 10;
    •       select DISTINCT *   
    • from staffchanges;
    • To display all columns in the order in which they are stored in a table, use an asterisk ( * ) in the SELECT clause. All rows will also be displayed, by default, unless you limit or subset them.
    • When you specify SELECT *, you can also use the FEEDBACK option in the PROC SQL statement, which writes the expanded list of columns to the SAS log
    • To indicate the maximum number of rows to be displayed, you can use the OUTOBS= option in the PROC SQL statement.
    • You can eliminate duplicate rows from your query results by using the keyword DISTINCT in the SELECT clause.
    11/13/09 SAS Techies 2009
    • proc sql noexec ;  
    • select empid, jobcode, salary
    • from sasuser.payrollmaster          
    • where jobcode contains 'NA‘
    • order by salary;
    • Select * from table;
    • Quit;
    • proc sql;       
    • validate
    •   select empid, jobcode, salary
    • from sasuser.payrollmaster          
    • where jobcode contains 'NA‘
    • order by salary;
    • When you are building a PROC SQL query, you might find it more efficient to check your query without actually executing it.
    • To verify the syntax and the existence of columns and tables that are referenced in the query without executing the query, use either of the following:
      • the NOEXEC option in the PROC SQL statement
      • the VALIDATE keyword before a SELECT statement.
      • The main difference between them is that the VALIDATE keyword only affects the SELECT statement that immediately follows it , whereas the NOEXEC option applies to all queries in the PROC SQL step.
    11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
    • ERRORSTOP instructs PROC SQL to stop executing the statements but to continue checking the syntax after it has encountered an error. ERRORSTOP has an effect only when SAS is running in batch or in noninteractive execution mode.
    • NOERRORSTOP instructs PROC SQL to execute the statements and to continue checking the syntax after an error occurs. NOERRORSTOP is useful if you want a batch job to continue executing SQL procedure statements after an error is encountered.
    • The LOOPS= option restricts the number of iterations of the inner loop in PROC SQL. By setting a limit, you can prevent queries from consuming excessive resources.
    • You can use the PROMPT | NOPROMPT option to modify the effect of the LOOPS= option so that you are prompted to stop or continue processing when the limit set by the LOOPS= option is reached.
    11/13/09 SAS Techies 2009
    • To examine data stored in two tables we use a SQL join
    • If there is a variable in both tables that is common then a SQL Join would display both of them.
    • Use table.column for referring the table…(do not require libname)
    • Use Aliases when if the Table names are the same
    proc sql; select bldginfo. id,lastname, building,room,extension from emplib.payroll, emplib . bldginfo where bldginfo. id=payroll.id order by bldginfo. lastname; proc sql; select * from emplib.payroll,emplib.bldginfo where bldginfo. id=payroll.id order by lastname; 11/13/09 SAS Techies 2009
    • Joins combine tables horizontally (side by side) by combining rows.
    • The tables being joined are not required to have the same number of rows or columns.
    11/13/09 SAS Techies 2009
    • A Cartesian product includes all columns from the source tables; columns that have common names are not overlaid.
    11/13/09 SAS Techies 2009
    • You can combine a maximum of 32 tables in a single inner join
    • The join condition that is specified in the WHERE clause often contains the equal ( = ) operator, but the expression may contain one or more other operators instead.
    • An inner join combines and displays only the rows from the first table that match rows from the second table, based on the matching criteria (also known as join conditions) that are specified in the WHERE clause.
    11/13/09 SAS Techies 2009
    • Conceptually, PROC SQL follows these steps to process a join:
      • builds a Cartesian product of rows from the indicated tables
      • evaluates each row in the Cartesian product, based on the join conditions specified in the WHERE clause (along with any other subsetting conditions), and removes any rows that do not meet the specified conditions
      • if summary functions are specified, summarizes the applicable rows
      • returns the rows that are to be displayed in output.
    11/13/09 SAS Techies 2009
    • To enable PROC SQL to distinguish between same-named columns from different tables, you use qualified column names. To create a qualified column name, you prefix the column name with its table name.
    11/13/09 SAS Techies 2009
    • An outer join combines and displays all rows that match across tables, based on the specified matching criteria (also known as join conditions), plus some or all of the rows that do not match .
    • In all three types of outer joins (left, right, and full), the columns in the result (combined) row that are from the unmatched row are set to missing values.
    11/13/09 SAS Techies 2009
    • A left outer join retrieves all rows that match across tables , based on the specified matching criteria (join conditions), plus nonmatching rows from the left table (the first table specified in the FROM clause).
    11/13/09 SAS Techies 2009
    • A right outer join retrieves all rows that match across tables , based on the specified matching criteria (join conditions), plus nonmatching rows from the right table (the second table specified in the FROM clause).
    11/13/09 SAS Techies 2009
    • A full outer join retrieves both matching rows and nonmatching rows from both tables.
    11/13/09 SAS Techies 2009
    • The COALESCE function overlays the specified columns by
      • checking the value of each column in the order in which the columns are listed
      • returning the first value that is a SAS nonmissing value.
      • If all returned values are missing, COALESCE returns a missing value.
    11/13/09 SAS Techies 2009
    • proc sql; 
    • create table work.miles as  
    • select membertype, 
    • sum(milestraveled) as TotalMiles
    • from sasuser.frequentflyers  
    •   group by membertype;
    • Quit;
    • To create a new table from the results of a query, use a CREATE TABLE statement that includes the keyword AS and the clauses that are used in a PROC SQL query: SELECT , FROM , and any optional clauses, such as ORDER BY.
    • The CREATE TABLE statement stores your query results in a table instead of displaying the results as a report.
    11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
    • proc sql;       
    • create table work.departments(
    • Dept char(20)
    • label='Department' ,
    • Code integer label='Dept Code',
    • Manager varchar(20),
    • AuditDate num format=date9. );
    • In the CREATE TABLE statement, a column specification may optionally include one or more of the following SAS column modifiers : INFORMAT= , FORMAT= , and LABEL= . Column modifiers, if used, are specified at the end of the column specification.
    • There is NO LENGTH modifier…drop the column name and recreate it.
    11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
    •   proc sql;       
    • create table work.employees          
    • (ID char (5) primary key ,          
    • Name char(10),          
    • Gender char(1) not null check(gender in ('M','F') ),          
    • HDate date label='Hire Date');
    11/13/09 SAS Techies 2009
    • proc sql;       
    • select empid, salary,
    • (salary/sum(salary)) as Percent
    • format=percent8.2    from sasuser.payrollmaster 
    • where jobcode contains 'NA' ;
    • proc sql;
    • select jobcode, avg(salary) as AvgSalary
    •   format=dollar11.2 
    • from sasuser.payrollmaster
    • group by jobcode 
    • having avg(salary) >  (select avg(salary)  from sasuser.payrollmaster);
    • Sometimes, when you use a summary function in a SELECT clause or a HAVING clause, PROC SQL must remerge data (make two passes through the table). Remerging requires additional processing time and is often unavoidable.
    • PROC SQL offers another type of expression that can be used for subsetting in WHERE and HAVING clauses: a query-expression or subquery .
    • A subquery is a query that is nested in, and is part of, another query. A PROC SQL query may contain subqueries at one or more levels.
    • A subquery selects one or more rows from a table, then returns single or multiple values to be used by the outer query. The subquery shown above is a single-value subquery ; it returns a single value, the average salary from the table Sasuser.Payrollmaster , to the outer query. A subquery can return values for multiple rows but only for a single column .
    SAS Techies 2009
    • noncorrelated subquery that returns a single value
    • select jobcode, avg(salary) as AvgSalary
    •   format=dollar11.2 
    • from sasuser.payrollmaster
    • group by jobcode 
    • having avg(salary) >  (select avg(salary)  from sasuser.payrollmaster);
    • multiple-value subqueries that return more than one value (row) to the outer query .
    • select empid, lastname, firstname,city
    • from sasuser.staffmaster
    • where empid in (select empid  
    • From sasuser.payrollmaster
    • where month(dateofbirth)=2) ;
    • Non-correlated sub query is a self-contained sub query that executes independently of the outer query
    • PROC SQL always evaluates a noncorrelated subquery before the outer query. If a query contains noncorrelated subqueries at more than one level, PROC SQL evaluates the innermost subquery first and works outward, evaluating the outermost query last.
    • You should be using one of the following operators in the WHERE or HAVING clause that can handle multiple values:
      • the conditional operator IN
      • a comparison operator that is modified by ANY or ALL
      • the conditional operator EXISTS.
    SAS Techies 2009
  • SAS Techies 2009
    • proc sql;  
    • select lastname, firstname
    • from sasuser.staffmaster 
    •   where 'NA'=  (select jobcategory 
    • from sasuser.supervisors 
    •      where staffmaster.empid = supervisors.empid) ;
    • proc sql;   select lastname, firstname      from sasuser.staffmaster,  sasuser.supervisors where jobcategory ='NA‘ and staffmaster.empid = supervisors.empid
    • Correlated subqueries cannot be evaluated independently , but depend on the values passed to them by the outer query for their results.
    • Correlated subqueries are evaluated for each row in the outer query and, therefore, tend to require more processing time than noncorrelated subqueries.
    • Usually, a PROC SQL join is a more efficient alternative to a correlated subquery
    SAS Techies 2009
    • from sasuser.flightschedule as f, 
    • (select flightnumber, date boarded/passengercapacity*100      as pctfull 
    • from sasuser.marchflights) as m
    • where m.flightnumber=f.flightnumber           and m.date=f.date
    • An in-line view is a nested query that is specified in the outer query's FROM clause .
    • An in-line view selects data from one or more tables to produce a temporary (or virtual) table that the outer query then uses to select data for output.
    • Unlike a table, an in-line view exists only during query execution. Because it is temporary, an in-line view can be referenced only in the query in which it is defined. In addition, an in-line view can be assigned an alias but not a permanent name.
    SAS Techies 2009
  • SAS Techies 2009
    • Data Step
    • match-merge requires that the data be sorted
    • By default match-merge creates a data set
    • Resultant dataset already sorted
    • match-merge overlays the two common columns by default (pre- req- both should have same name and length)
    • SQL Joins
    • join does not require that you sort the data first.
    • By default, SQL inner join, creates only a report as output
    • If the order of rows in the output does not matter, the ORDER BY clause can be removed from the PROC SQL join. Without the ORDER BY clause, this join is more efficient, because PROC SQL does not need to make a second pass through the data.
    • SQL outer join does not overlay the two common columns by default. To overlay common columns, you must use the COALESCE function in the PROC SQL full outer join
    SAS Techies 2009
  • SAS Techies 2009
    • General integrity constraints enable you to restrict the values of columns within a single table. The following four integrity constraints can be used as general integrity constraints:
      • CHECK
      • NOT NULL
      • UNIQUE
      • PRIMARY KEY.
      • A PRIMARY KEY constraint is a general integrity constraint if it does not have any FOREIGN KEY constraints referencing it. A PRIMARY KEY used as a general constraint, is a shortcut for assigning the constraints NOT NULL and UNIQUE.
    • A Referential integrity constraint is created when a PRIMARY KEY integrity constraint in one table is referenced by a FOREIGN KEY integrity constraint in another table. There are two steps that must be followed to create a referential integrity constraint:
      • Define a PRIMARY KEY constraint on the first table.
      • Define a FOREIGN KEY constraint on other tables.
      • Integrity constraints
      • follow ANSI standards
      • cannot be defined for views
      • cannot be defined for historical versions of generation data sets.
    11/13/09 SAS Techies 2009
    • Views are useful because they
      • often save space (a view is usually quite small compared with the data that it accesses)
      • prevent users from continually submitting queries to omit unwanted columns or rows
      • ensure that input data sets are always current, because data is derived from tables at execution time
      • shield sensitive or confidential columns from users while enabling the same users to view other columns in the same table
      • hide complex joins or queries from users.
    • A PROC SQL view is a stored query that is executed when you use the view in a SAS procedure, DATA step, or function. A view contains only the descriptor and other information required to retrieve the data values from other SAS files (SAS data files, DATA step views, or other PROC SQL views) or external files (DBMS data files). The view contains only the logic for accessing the data, not the data itself.
    • Because PROC SQL views are not separate copies of data, they are referred to as virtual tables . They do not exist as independent entities like real tables
    11/13/09 SAS Techies 2009
    • When you are working with PROC SQL views, it is best to follow these guidelines:
      • Avoid using an ORDER BY clause in a view definition, which causes the data to be sorted every time the view is executed. Users of the view might differ in how or whether they want the data to be sorted, so it is more efficient to specify an ORDER BY clause in a query that references the view.
      • If the same data is used many times in one program or in multiple programs, it is more efficient to create a table rather than a view because the data must be accessed at each view reference. (This table can be a temporary table in the Work library.)
      • Avoid creating views that are based on tables whose structure might change. A view is no longer valid when it references a nonexistent column.
      • If a view resides in the same SAS data library as the contributing table(s), it is best to specify a one-level name in the FROM clause.
      • One advantage of PROC SQL views is that they can bring data together from separate sources. This enables views to be used to shield sensitive or confidential columns from some users while enabling the same users to view other columns in the same table.
    11/13/09 SAS Techies 2009
    • You can update the data underlying a PROC SQL view using the INSERT , DELETE , and UPDATE statements under the following conditions:
      • You can only update a single table through a view. The table cannot be joined or linked to another table, nor can it contain a subquery.
      • You can update a column using the column's alias, but you cannot update a derived column (a column that is produced by an expression).
      • You can update a view that contains a WHERE clause. The WHERE clause can be specified in the UPDATE clause or in the view. You cannot update a view that contains any other clause such as an ORDER BY or a HAVING clause.
      • You cannot update a summary view (a view that contains a GROUP BY clause).
      • Updating a view does not change the stored instructions for the view. Only the data in the underlying table(s) is updated.
    11/13/09 SAS Techies 2009
    • Combining data horizontally refers to the process of merging or joining multiple data sets that contain different but related information into one data set.
    • The terms combining data horizontally and performing a table lookup are synonymous.
    • In order to combine data horizontally, you must be able to match observations from each input data source
          11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
    • data mylib.employees_new;       
      • set mylib.employees;       
      • if IDnum=1001 then Birthdate='01JAN1963'd;       
      • else if IDnum=1002 then Birthdate='08AUG1946'd;       
    • run;
    • data mylib.employees_new;       
      • array birthdates{1001:1003} _temporary_ ('01JAN1963'd '08AUG1946'd '23MAR1950'd };       
      • set mylib.employees;       
      • Birthdate=birthdates(IDnum);    
    • run;
    • You can use the following techniques to hard-code lookup values into your program:
      • the IF-THEN/ELSE statement
      • SAS arrays
      • user-defined SAS formats.
    11/13/09 SAS Techies 2009
      • It is possible to create identical results with a DATA step match-merge and a PROC SQL inner join.
      • Although you can use the MERGE statement to combine data from sources that have any type of relationship, this technique might NOT produce the desired results when you are working with a many-to-many match.
      • When the data sets are merged in a DATA step, the observations are matched and combined sequentially. Once an observation is read, it is never re-read. That is, the DATA step MERGE statement does not create a Cartesian product.
      • Therefore, the DATA step MERGE statement is probably NOT an appropriate technique to use for performing lookup operations when you are working with a many-to-many match.
    11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
      • To use the SET statement with the KEY= option to perform a lookup operation, your lookup values must be stored in a SAS data set that has an index. This technique is appropriate only when you are working with one-to-one matches, and you can use it with a lookup table of any size. It is possible to return multiple values with this technique, and you can use other DATA step syntax with it as well.
      • When SAS encounters the SET statement that includes the KEY= option, there must already be a value in the PDV for the value or values of the key variable(s) on which the KEY= index is built. SAS can then use the index to retrieve an observation that has a value for the key variable that matches the key value from the PDV.
    11/13/09 SAS Techies 2009
    • Dictionary tables are special, read-only SAS tables that contain information about SAS data libraries, SAS macros, and external files that are in use or available in the current SAS session.
    • Dictionary tables also contain the settings for SAS system options and SAS titles and footnotes that are currently in effect.
    • Dictionary tables are
      • created each time they are referenced in a SAS program
      • updated automatically
      • limited to read-only access.
    • Dictionary tables are commonly used to monitor and manage SAS sessions because the data is easier to manipulate than the output from procedures such as PROC DATASETS.
    select memname  from sashelp.vcolumn where libname='SASUSER‘ and name='EMPID'; 11/13/09 SAS Techies 2009
      • proc format; 
      • value dates (multilabel)
      • '01jan2000'd - '31mar2000'd = '1st Qrtr‘
      • '01apr2000'd - '30jun2000'd = '2nd Qtr‘
      • '01jul2000'd - '30sep2000'd = '3rd Qtr‘
      • '01oct2000'd - '31dec2000'd = '4th Qtr‘
      • '01jan2000'd - '30jun2000'd = 'First Half of Year‘
      • '01jul2000'd - '31dec2000'd = 'Second Half of Year'; 
      • run;
    • Suppose you want to create a format that groups dates into overlapping categories. In the table below, notice that each month appears in two groups.
    11/13/09 SAS Techies 2009
    • Pictures are specified with three types of characters:
      • digit selectors
      • message characters
      • directives.
    • Digit selectors are numeric characters (0 through 9) that define positions for numeric values. If you use nonzero digit selectors, zeros are added to the formatted value as needed. If you use zeros as digit selectors, no zeros are added to the formatted value.
    11/13/09 SAS Techies 2009
    • libname library 'c:sas ewfmt';    
    • proc format lib=library fmtlib ;   
    • run;
    • options fmtsearch=(rpt prod.newfmt);
    • When you have created a large number of permanent formats, it can be easy to forget the exact spelling of a specific format name or its range of values. Remember that adding the keyword FMTLIB to the PROC FORMAT statement displays a list of all the formats in the specified catalog, along with descriptions of their values.
    • If you store formats in libraries or catalogs other than those in the default search path, you must use the FMTSEARCH= system option to tell SAS where to find your formats.
    • OPTIONS FMTERR | NOFMTERR;
      • FMTERR specifies that when SAS cannot find a specified variable format, it generates an error message and stops processing. Substitution does not occur.
      • NOFMTERR replaces missing formats with the w . or $ w . default format and continues processing
    11/13/09 SAS Techies 2009
    • You can create a format from a SAS data set that contains value information (called a control data set) using the CNTLIN= option.
    • Rules for Control Data Sets
      • When you create a format using programming statements, you specify the name of the format, the range or value, and the label for each range or value as in the VALUE statement below:
      • value rainfall 0='none'; The control data set you use to create a format must contain variables that supply this same information. That is, the data set specified in the CNTLIN= option
      • must contain the variables FmtName, Start, and Label , which contain the format name, value or beginning value in the range, and label.
      • must contain the variable End if a range is specified . If there is no End variable, SAS assumes that the ending value of the format range is equal to the value of Start.
      • must contain the variable Type for character formats , unless the value for FmtName begins with a $.
      • must be sorted by FmtName if multiple formats are specified.
    11/13/09 SAS Techies 2009
  • 11/13/09 SAS Techies 2009
    • proc format lib=sasuser cntlout =sasuser.fmtdata; 
    • select $airport; 
    • run;
    • When you use the CNTLOUT= option, SAS creates an output data set that has many variables for storing information about the format.
    11/13/09 SAS Techies 2009