1 / 75

Chapter 4: Introduction to Lookup Techniques

Chapter 4: Introduction to Lookup Techniques. Chapter 4: Introduction to Lookup Techniques. Objectives. Define table lookup. List table lookup techniques. Lookup Values. Table Lookups. lookup. Data Values. Lookup values for a table lookup can be stored in the following: array

Télécharger la présentation

Chapter 4: Introduction to Lookup Techniques

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4: Introduction to Lookup Techniques

  2. Chapter 4: Introduction to Lookup Techniques

  3. Objectives • Define table lookup. • List table lookup techniques.

  4. Lookup Values Table Lookups lookup Data Values • Lookup values for a table lookup can be stored in the following: • array • hash object • format • data set Lookup techniques include the following: • array subscript value • hash object key value • FORMAT statement, PUT function • MERGE, SET/SET, join

  5. 4.01 Multiple Choice Poll • Which of these is an example of a table lookup? • You have the data for January sales in one data set, February sales in a second data set, and March sales in a third. You need to create a report for the entire first quarter. • You want to send birthday cards to employees. The employees’ names and addresses are in one data set and their birthdates are in another. • You need to calculate the amount each customer owes for his purchases. The price per item and the number of items purchased are stored in the same data set.

  6. 4.01 Multiple Choice Poll – Correct Answer • Which of these is an example of a table lookup? • You have the data for January sales in one data set, February sales in a second data set, and March sales in a third. You need to create a report for the entire first quarter. • You want to send birthday cards to employees. The employees’ names and addresses are in one data set and their birthdates are in another. • You need to calculate the amount each customer owes for his purchases. The price per item and the number of items purchased are stored in the same data set.

  7. Overview of Table Lookup Techniques • Arrays, hash objects, and formats provide an in-memory lookup table. • The DATA step MERGE statement, multiple SET statements in the DATA step, and SQL procedure joins use lookup values that are stored on disk.

  8. Chapter 4: Introduction to Lookup Techniques

  9. Objectives • Describe arrays as a lookup technique. • Describe hash objects as a lookup technique. • Describe formats as a lookup technique.

  10. 4.02 Multiple Answer Poll • Which techniques do you currently use when you perform table lookups with a single data set? • Arrays • Hash object • Formats • None of the above

  11. 1 2 3 4 Overview of Arrays ... An array is similar to a numbered row of buckets.

  12. 1 2 3 4 Overview of Arrays ... • An array is similar to a numbered row of buckets. • SAS puts a value in a bucket based on the bucket number.

  13. 1 2 3 4 Overview of Arrays • An array is similar to a numbered row of buckets. • SAS puts a value in a bucket based on the bucket number. • A value is retrieved from a bucket based on the bucket number.

  14. Overview of Arrays DATAdata-set-name; ARRAYarray-name{ subscript } <$><length> <array-elements> <(initial-value-list)>; < READ statement (s)> new-variable=array-name{subscript-value}; RUN; The ARRAY statement associates variables or initial values to be retrieved using the array name and a subscript value. The assignment statement retrieves values from the array based on the value of the subscript. General form of the ARRAY statement: The READ statement can be the SET, MERGE or INFILE/INPUT statement.

  15. Overview of Arrays data country_info; array Cont_Name{91:96} $ 30 _temporary_ ('North America', ' ', 'Europe', 'Africa', 'Asia', 'Australia/Pacific'); set orion.country; Continent=Cont_Name{Continent_ID}; run; The assignment statement retrieves values from the array based on the value of the subscript. The ARRAY statement associates variables or initial values to be retrieved using the array name and a subscript value. p304d01

  16. Setup for the Poll p304d01 data country_info; array Cont_Name{91:96} $ 30 _temporary_ ('North America', ' ', 'Europe', 'Africa', 'Asia', 'Australia/Pacific'); set orion.country; Continent=Cont_Name{Continent_ID}; run;

  17. 4.03 Multiple Choice Poll • In p304d01, how many elements are in the array Cont_name? • 0 • 5 • 6 • unknown

  18. 4.03 Multiple Choice Poll – Correct Answer • In p304d01, how many elements are in the array Cont_name? • 0 • 5 • 6 • unknown

  19. Overview of a Hash Object Key Data Data ... A hash object is similar to rows of buckets that are identified by the value of a key.

  20. Overview of a Hash Object Key Data Data ... • A hash object is similar to rows of buckets that are identified by the value of a key. • SAS puts value(s) in the data bucket(s) based on the value(s) in the key bucket.

  21. Overview of a Hash Object Key Data Data • A hash object is similar to rows of buckets that are identified by the value of a key. • SAS puts value(s) in the data bucket(s) based on the value(s) in the key bucket. • Value(s) are retrieved from the data bucket(s) based on the value(s) in the key bucket.

  22. Overview of Hash Objects The syntax within the DOgroup defines and canpopulate the hash object. DATAdata-set-name; < READ statement(s) > IF _N_=1 THEN DO; DECLARE HASH object-name(<attribute:value>); object-name.DEFINEKEY('key-name'); object-name.DEFINEDATA('data-name'); object-name.DEFINEDONE(); END; return-code=object-name.FIND(<key: value>); RUN; The FIND method retrieves the data value based on the key value. General form of the hash object: The READ statement can be the SET, MERGE, or INFILE/INPUT statement.

  23. Overview of Hash Objects The syntax within the DO group defines and populates the hash object. data country_info; length Continent_Name $ 30; if _N_=1 then do; declare hash Cont_Name(dataset:'orion.continent'); Cont_Name.definekey('Continent_ID'); Cont_Name.definedata('Continent_Name'); Cont_Name.definedone(); end; set orion.country; rc=Cont_Name.find(key:Continent_ID); if rc=0; run; The FIND method retrieves the data value based on the key value. p304d02

  24. Setup for the Poll p304d02 data country_info; length Continent_Name $ 30; if _N_=1 then do; declare hash Cont_Name(dataset:'orion.continent'); Cont_Name.definekey('Continent_ID'); Cont_Name.definedata('Continent_Name'); Cont_Name.definedone(); end; set orion.country; rc=Cont_Name.find(key:Continent_ID); if rc=0; run;

  25. 4.04 Multiple Choice Poll • In p304d02, how many times do the statements in the DO group execute? • only once • once for every observation in the data set orion.country • once for every observation in the data set orion.continent

  26. 4.04 Multiple Choice Poll – Correct Answer • In p304d02, how many times do the statements in the DO group execute? • only once • once for every observation in the data set orion.country • once for every observation in the data set orion.continent

  27. Data Value Label Overview of a Format ... A format is similar to rows of buckets that are identified by the data value.

  28. Data Value Label Overview of a Format ... • A format is similar to rows of buckets that are identified by the data value. • SAS puts data values and label values in the buckets when the format is used in a FORMAT statement, PUT function, or PUT statement.

  29. Data Value Label Overview of a Format • A format is similar to rows of buckets that are identified by the data value. • SAS puts data values and label values in the buckets when the format is used in a FORMAT statement, PUT function, or PUT statement. • SAS uses a binary search on the data valuebucket in order to return the value in the label bucket.

  30. Overview of a Format The FORMAT stepcompiles the formatand stores it on disk. PROC FORMAT; VALUE <$>fmtnamerange-1=label-1 . . . range-n=label-n; RUN; DATAdata-set-name; < READ statement(s)>; new-variable=PUT(variable,fmtname.); RUN; When the PUT function executes, the format is loaded into memory, and a binary search is used to retrieve the format value. General form of the user-defined format: The READ statement can be the SET, MERGE, or INFILE/INPUT statement.

  31. Overview of a Format The FORMAT step compiles the format and stores it on disk. proc format; value Cont_Name 91='North America' 93='Europe' 94='Africa' 95='Asia' 96='Australia/Pacific'; run; data country_info; set orion.country; Continent=put(Continent_ID,Cont_Name.); run; When the PUT function executes, the format is loaded into memory, and a binary search is used to retrieve the format value. p304d03

  32. Chapter 4: Introduction to Lookup Techniques

  33. Objectives • List methods for combining data horizontally. • Use multiple SET statements to combine data horizontally. • Compare methods for combining SAS data sets.

  34. Combining Data Horizontally • DATA step techniques for combining data horizontally include using the following: • MERGE statement • multiple SET statements • UPDATE statement • MODIFY statement In addition, you can use the SQL procedure with an inner or outer join.

  35. 4.05 Multiple Answer Poll • Which techniques do you currently use when you perform table lookups with multiple data sets? • MERGE statement • Joins • Multiple SET statements • UPDATE statement • MODIFY statement • None of the above

  36. Overview of Merges and Joins By Value(s) Data Data By Value(s) Data Data The DATA step MERGE and the SQL join operators are similar to multiple stacks of buckets that are referred to by the value of one or more common variables.

  37. Continent_ID DATA Step MERGE Statement DATAdata-set-name; MERGESAS-data-sets; BYvariables; RUN; Continent_ID Continent_ID General form of the DATA step merge: Matches on equal values for like-named variables:

  38. DATA Step MERGE Statement proc sort data=orion.country out=country; by Continent_ID; run; data country_info; merge country orion.continent; by Continent_ID; run; Matches on equal values for like-named variables p304d04

  39. Setup for the Poll p304d04 proc sort data=orion.country out=country; by Continent_ID; run; data country_info; merge country orion.continent; by Continent_ID; run;

  40. 4.06 Multiple Choice Poll • In p304d04, if the data set country has seven observations and the data set orion.continent has five observations, what stops the execution of the DATA step? • end of file for work.country, the data set with the most observations • end of file for orion.continent, the last data set listed in the MERGE statement • end of file for the data set that contains the final value of the BY variable Continent_ID

  41. 4.06 Multiple Choice Poll – Correct Answer • In p304d04, if the data set country has seven observations and the data set orion.continent has five observations, what stops the execution of the DATA step? • end of file for work.country, the data set with the most observations • end of file for orion.continent, the last data set listed in the MERGE statement • end of file for the data set that contains the final value of the BY variable Continent_ID

  42. The SQL Procedure You can use an SQL procedure inner or outer join to create a SAS data set. General form of the SQL procedure CREATE TABLE statement with an inner join: PROC SQL; CREATETABLESAS-data-setAS SELECTcolumn-1, column-2,… ,column-n FROM table-1, table-2,…,table-n WHERE joining criteria ORDER BY sorting criteria; QUIT; Performs an inner join based on the WHERE criteria

  43. The SQL Procedure proc sql; create table country_info as select country.*, Continent_Name from orion.country, orion.continent where country.Continent_ID= continent.Continent_ID; order by country.Continent_ID; quit; Performs an inner join where the Continent_ID values from both data sets are equal p304d05

More Related