Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression.

Similar presentations


Presentation on theme: "1 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression."— Presentation transcript:

1 1 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression 4.4 Creating New Columns by Recoding Values 4.5 Joining Tables 4.6 Joining Tables Including Non-Matching Rows (Self-Study)

2 2 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression 4.4 Creating New Columns by Recoding Values 4.5 Joining Tables 4.6 Joining Tables Including Non-Matching Rows (Self-Study)

3 3 Objectives State the function of the query task. Name the areas within the Query Builder dialog. State the function of the areas within the Query Builder dialog.

4 4 Query Builder The Query Builder is used to extract data from one or more tables according to the criteria specified by the user.

5 5 Query Builder: Select Data The Select Data tab is used to select which columns are included and what their roles are in the query.

6 6 Query Builder: Filter Data The Filter Data tab is used to retrieve rows that meet certain criteria, based on existing or calculated columns.

7 7 Query Builder: Computed Columns The Computed Columns button is used to add new columns, either by building an expression or recoding an existing column.

8 8 Query Builder: Tables and Joins The Tables and Joins window is used to join multiple tables based on a relationship between columns.

9 9 Query Builder: Parameters The Parameters button enables you to define a query that returns different results based on values you provide at runtime.

10 10 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression 4.4 Creating New Columns by Recoding Values 4.5 Joining Tables 4.6 Joining Tables Including Non-Matching Rows (Self-Study)

11 11 Objectives Apply a filter in a query. Exclude columns in a query. Reorder columns in a query. Modify a column’s properties.

12 12 Selecting Both Rows and Columns

13 13 Selecting Columns By default, a query starts out empty. You must add any columns to be included in the query to the Select Data tab. Drag and drop or right- click to add columns to the Select Data tab. Modify a column’s properties.

14 14 Filtering Rows The process of specifying which rows to retrieve in a query is called setting a filter and is done in the Filter Data tab. 2) Drop the column in here. 1) Drag a column from here.

15 15 Setting a Filter When a column is dropped on the Filter Data tab, the Edit Filter dialog opens. Type the value(s) or expression. Choose the type of comparison.

16 16 Setting an Advanced Filter Select New Advanced Filter... to open the Advanced Expression Editor and create a new filter. Create an advanced filter to build your own condition.

17 17 Advanced Expression Editor: Functions 1) Select the type of function. 2) Select a specific function.

18 18 Advanced Expression Editor: Data 1) Select a column to view distinct values. 2) Select values to include in the expression.

19 19 Combining Filters By default, multiple filters are combined with AND. The Filter Combination window can be used to further customize the way multiple filters are evaluated.

20 20 Final Results: Selecting Rows and Columns

21 21 Using Query Results in Tasks Tables generated from queries can serve as the active data source for follow-up tasks.

22 22 Scenario LLB wants to determine how many non-U.S. companies supply products to LLB. The report should reflect the following: the total number of suppliers in each country, excluding the United States countries with the most suppliers listed first

23 23 Scenario Partial Output

24 24 This demonstration illustrates how to set a filter and exclude columns in the Query Builder task. Selecting Columns and Filtering Rows

25 25 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression 4.4 Creating New Columns by Recoding Values 4.5 Joining Tables 4.6 Joining Tables Including Non-Matching Rows (Self-Study)

26 26 Objectives Define a new column of data in a query by building an expression.

27 27 Computing a New Column Revenue = Quantity * SellPrice

28 28 Computed Columns Create new columns.

29 29 Advanced Expression Editor 1) Choose a method for computing the new column. 2) Use the Advanced Expression Editor to build the expression.

30 30 Computed columns appear in the left pane and can be used in a filter on the Filter Data tab or as an input to another computed column. New Computed Columns

31 31 Scenario LLB wants to know the projected inventory of each product at any given moment. The products table is constantly changing with daily updates for the number of units in stock and the number of units on order. ProjectedInventory = UnitsInStock + UnitsOnOrder

32 32 This demonstration illustrates how to use the Advanced Expression Editor to define a column of data in the Query Builder task. Creating a Column with an Expression

33 33 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression 4.4 Creating New Columns by Recoding Values 4.5 Joining Tables 4.6 Joining Tables Including Non-Matching Rows (Self-Study)

34 34 Objectives Recode individual values or a range of values in a column.

35 35 Recoded Columns New columns can also be calculated by recoding values from an existing column.

36 36 Recoded Values Recoding a column enables you to assign a value to a new column based on the value of an existing column. When State='CA' Then Region = 'West' TRUE Then Region = 'Central' TRUE Then Region = 'East' TRUE When State='NY' FALSE When State='MI' FALSE

37 37 Recode a Column When you recode a column, a prompt will appear to select the existing column to be used to generate the new column.

38 38 Specify a Replacement 2) Use Replace Values or Replace a Range to map the recoded values to the original data. 1) Select Add… to assign the new recoded values.

39 39 Recoding a Column Assign values for all other cases. Select the appropriate type for the new column.

40 40 Using the New Recoded Column The new recoded column is automatically included in the Select Data tab. The new column can also be used in the Filter Data or Sort Data tabs as well. To sort by a column, drag the column into the Sort Data tab and select the Sort Direction.

41 41 Scenario LLB wants to know how the unit costs for the products are distributed within each food category. Rank each product into three unique groups based on the unit cost: $0 to $14.99, $15 to $29.99, and $30 and Above.

42 42 Partial Table Analysis Results

43 43 This demonstration illustrates the use of the Replace Values tab in a query to create a new column based conditionally upon an existing column. Creating a New Column by Recoding Values

44 44 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression 4.4 Creating New Columns by Recoding Values 4.5 Joining Tables 4.6 Joining Tables Including Non-Matching Rows (Self-Study)

45 45 Objectives Join multiple tables by common columns. Include only matching rows.

46 46 Joining Tables Joining tables enables you to extract and process data from more than one table at a time.

47 47 Joining Tables Matching rows only

48 48 Joining Tables Matching rows only

49 49 Adding Tables Select Add Tables… to include additional data sources in the query.

50 50 Tables and Joins Window 1) Select Join… to access the Tables and Joins window. 2) Use the Tables and Joins window to specify how multiple tables relate.

51 51 Modify Join Window The join criteria can be modified to include non-matching rows from either or both tables.

52 52 Query Builder: Selecting a Server Use the Result Options window to specify the SAS server that should be used to execute the query.

53 53 Join Results From USCustomerDatabaseLong From ItemsOrdered

54 54 Scenario LLB wants to compute the total profits generated from each order. The profit that LLB earns from each order can be calculated as the difference between the selling price and the unit cost multiplied by the quantity of items ordered. LLB stores this quarter's information about its suppliers, products, and orders in three separate tables.

55 55 Scenario: Separate Tables

56 56 Scenario: Joined Table with New Column Profit = (SellPrice – UnitCost) * Quantity

57 57 This demonstration illustrates how to join multiple tables and store the result in a data table. Joining Tables

58 58 Complete the Chapter 4 exercises starting on page 4-68. Detailed solutions are provided starting on page 4-73. Exercises

59 59 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression 4.4 Creating New Columns by Recoding Values 4.5 Joining Tables 4.6 Joining Tables Including Non-Matching Rows (Self-Study)

60 60 Objectives Perform different join types.

61 61 Joining Tables Types of Joins: Matching Rows Only (SAS Enterprise Guide default) –produces results where only the rows from one table that have a corresponding match in every other table are returned. All Rows from one or both tables –produces results where all of the matched rows from both tables and the unmatched rows from at least one table are returned. All Rows from AAll Rows from A and BAll Rows from B A B

62 62 Review: Matching Rows Only

63 63 Including Non-Matching Rows All rows from CustomerDatabase and ItemsOrdered

64 64 Including Non-Matching Rows All rows from CustomerDatabase

65 65 Including Non-Matching Rows All rows from ItemsOrdered

66 66 Review: Modify Join Window The join criteria can be modified to include non-matching rows from either or both tables.

67 67 Modifying a Join: Results Results include all orders that are in the ItemsOrdered table, whether customer information in the CustomerDatabase table exists or not.

68 68 Isolating Non-Matching Rows The query can also include a filter to isolate the non-matching rows from one or both tables. Orders placed by customers who do not exist in the CustomerDatabase table

69 69 Scenario When products are discontinued, they are removed from the products table. LLB wants to generate a query on a regular basis to identify orders placed on discontinued products.

70 70 This demonstration illustrates how to change the join type to include non-matching rows in a query. Joining Tables Including Non-Matching Rows

71 71 Complete Exercise 6 starting on page 4-71. Detailed solutions are provided starting on page 4-82. Exercises


Download ppt "1 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression."

Similar presentations


Ads by Google