0% found this document useful (0 votes)
53 views

I Hate SQL! Tips and Tricks For Using The Query Window: Dianne Louise Rhodes, Westat, Rockville, MD

SQL book

Uploaded by

sahil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

I Hate SQL! Tips and Tricks For Using The Query Window: Dianne Louise Rhodes, Westat, Rockville, MD

SQL book

Uploaded by

sahil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

I Hate SQL!

Tips and Tricks for Using the Query Window


Dianne Louise Rhodes, Westat, Rockville, MD

ABSTRACT SIMPLE JOIN AND REPORT

This paper introduces the online SAS® Tool, the SQL Query To register the database, at the Windows command line, select
Window. Using the Query Window you can build complex SQL Start | Settings | Control Panel. Click on ODBC Data Sources.
queries and reports, with only a little knowledge of SQL and Proc Select Add, then chose the Miicrosoft Access Driver. When the
Report. This is a useful tool for checking and debugging Data Source Name window appears, type in the location on your
programs that use relational data. It provides a GUI to link system, or chose Select to browse the system. Once the
multiple tables and generate, modify, and save Proc SQL and datasource is added, define the library you will be using:
Proc Report statements interactively. The output is delivered in
pages, allowing you to scroll through the pages or go to a libname SQL ODBC DATASRC="nwind.mdb";
particular page. The queries can take advantage of the prompt run;
feature, which allows you to parameterize a query for a value
entered in a dialog box.
Go to the Query Window from the Tools PMENU | Query. Select
SAS Version 8.0 for Windows was used to produce the screens the SQL library. This will bring up a list of the available Tables.
for this paper. Select the Orders and Customers tables. It’s important to note
that you must select all the tables you will need for your query
here. If you get into building the query and realize you have
GETTING STARTED forgotten to include a table, you have to start all over again.

The SQL Query window is an interactive point and click interface,


through which a novice user can build, save, and run fairly
sophisticated SQL queries. These queries can be passed to the
SQL procedure or to PROC REPORT for processing.

There are a number of ways to invoke the SQL Query window:


• From the command line, type Query
• From the menu, select Tools | Query

The first program menu (PMENU) lets you select the libraries you
will use in building your queries. Click “OK” and you will get the
SQL Query Tables window.

The first window lets you select the library or table source. Once
selected, a drop down list of available tables appears, and you
can select the tables for the query you are building.

This window allows you to choose from any library references


you have defined which can be SAS datasets or views. If you
have SAS/Access installed, you can query DBMS data. For
example, you can query Oracle tables or views using a libname
engine. Using ODBC, you can access other PC file formats. When you have selected the tables needed for your query, click
on “OK”. This takes you to the Column Query window. Here you
can select the columns that you want to include in your query.
Be sure that you have selected all the tables you will need to
You can add formats, change the order of columns, and build a
build your query. If you start building the query and have omitted
new column.
a table, you will have to go back to this point and start all over
again.
Version 8 allows you to toggle back and forth between the Query
Columns Window and the Query Tables Window. However, I
WORKING THROUGH SOME EXAMPLES was not able to add data sources after leaving the Query Tables
window.
The SAS online documentation contains examples you can use
with the SAMPLE data available with SAS. I have chosen to From the Customers table, select Customer ID, Company Name.
develop some examples using the North Wind database that is From the Orders table, select Order Date and Required Date.
contained on the MS® Access installation CD. This will give
some good examples of working with a relational database.
Now you need to tell SQL how to join these two tables. Go to the Notice that you have a number of options from this window,
View PMENU and click on Join Type. Pick Matched Join. This including the option to create a table or a view. This is a very
will bring up the Join Window. This window displays the columns useful way to check data. You can also save the Query and
from each table that SAS identifies as likely keys. Select a execute it later, or use the source code to build other queries.
column from the left and the right tables. For our example, select You can save a Query as a catalog entry, as an external file, or
Customer ID from both tables, and click on “OK”. Optionally you as a source entry.
can set the join type to an inner join.
You can also view the Query with the Preview Window. Click on
When you build a Query with two or more tables, if you forget to the Windows Close button to go back to your query.
provide the necessary where clause, SAS will give you an error
prompt to build a where clause. Once you are satisfied with the Query you have built, you have a
number of ways of looking at the output. You can output to SQL
by selecting Tools | Run Query | Run Immediate, which will cause
the query to execute and all the output to be dumped to the
Output Window. My preference is to use the Report Option.
Select Tools | Run Query | Design Report | Default Report. This
gives you greater control of your output and allows you to go
through it page by page. It directs your output to PROC
REPORT and generates report statements.

Notice a few things with this report you have created. You can
navigate the report using the View PMENU. Both Order Date and
Required Date are datetime variables. You would like to change
this to just the date. There are several ways to do this, but the
easiest is to double click on the Column name in the Report
Window. This brings up the column definition window.

Now you have enough SQL to run a report. There are a couple of Change the format from Datetime20 to Datetime8. This looks
ways to see the SQL you have just created. You can check out right, but to save the change permanently, you need to go back
the SQL via the Tools PMENU, and select Show Query. to the Query Columns Window. Select the variable, then click on
Column Formats. If you don’t know what format you want, click
on the arrow and that will give you a view table of formats. Run MORE COMPLEX QUERIES AND REPORTS
the report again. To see the Proc Report statements that are
generated, from the Report Window, Select Tools | Report In order to develop a more complex query, I wanted to join three
Statements. Notice that you can change the width of any column tables. Many of the table names in the North Wind database
in the Report Window by double clicking on the column. contain spaces. I discovered that the SQL query window did not
like these tables with spaces in the names. As a quick work
around, I copied the data as follows:

data test ;
set sql.'Order Details'n;
*note the n literal;
run;

data sql.order_detail ;
set test ;
run;

JOIN THREE TABLES

Go to the Query Tables Window and select Customers, Orders,


and the table created in the previous step, order_detail. Click on
“OK” and go to the Query Columns Window. From the
Customers table, select Customer ID, and Contact Name. From
the Orders table, select Order ID and Shipped Date. From the
order_detail table, select Order ID, Product ID, UnitPrice,
Quantity and Discount.
To save your report statements, from the Source window use the
File PMENU to save the code. In order to join these three tables, you will have to define a Where
ADDING A WHERE CLAUSE Statement. Click on the View PMENU, and select Where
Conditions for Subset. The Where expression you will build will
join Customers to Orders by Customer ID, and Orders to
You can use where clauses to define a subset of your data. In Order_detail by Order ID.
the process of checking data and debugging programs, I have
found using the Prompt facility when building a where clause to
be very useful. This allows you to run the same Query and
Report on various subsets of your data. Another option is to use
the Lookup facility, which gives you a drop down list of the values
of the subsetting variable present in your data.

To build a Where Clause with a prompt, close the Report


Window. In the Query Columns Window, Select View | Where
Conditions for Subset. Select Required Date. Select the
operator (GT) and <Prompt at Run-time.>. Then enter the prompt
you want the user to receive at run time. At run time, the user
can enter a value, or click on Lookup and get a drop down list of
the values present in the dataset.
CHANGING REPORT OPTIONS

You can change the default Report options. Go to Tools |


Options | Report. For example, you can check Headline and an BUILD A COMPUTED COLUMN
underscore will be added after the title of your report.
You can add computed columns easily from the Query Columns
Window. Click on Build a Column. This brings up the Build
Column Window. Select Quantity, then the operator “*”, then
UnitPrice to create a new column called Totcost. Use the
Column Attributes button to name, format and label the variable
Phone: (301) 315-5977
Email: [email protected]

TRADEMARKS

SAS®, SAS/Access®, SAS/Connect®, and SAS/SQL® are


registered trademarks of SAS Institute Inc., in the USA and other
countries. ® indicates USA registrations. Microsoft ® Access®
is a registered trademark of Microsoft.

Other brand and product names are registered trademarks or


trademarks of their respective companies.

GROUP BY COLUMN

To finish off this report, let’s add another feature. From the View
PMENU, select Groups for Summary Function. Select Customer
ID. Now you have a useful report for displaying information on
customer invoices. You can add a column to show the totals for
each customer id. If you want to get really fancy you can add
subtotals for each order. Go ahead, try it!

CONCLUSION

The SAS SQL Query Window provides a simple way to create


complex queries and reports. You can generate PROC SQL and
PROC REPORT statements, execute them, edit them, and save
them for other uses. With practice, you can create nice looking
reports with PROC REPORT without knowing PROC REPORT!

These features also present an opportunity to learn SQL and


REPORT first hand, and correct mistakes as you go along.

REFERENCES

SAS Institute Inc., (1999) SAS Online Doc ™, Version Eight,


Cary, NC: SAS Institute.

Steulpner, Janet (1999). “What your Mother Never Told You


About the Query Window,” in Proceedings of the 24th SAS Users
Group Internationals, pp. 342-344.

ACKNOWLEDGMENTS

I would like to thank all my colleagues at Westat and especially


Ian Whitlock for his inquiring mind and willingness to find a
solution to every problem. I’d also like to thank my colleagues
around the country and around the world from SAS-L for their
support during a particularly difficult year.

CONTACT INFORMATION

Comments, questions, and additions are welcomed.


Contact the author at:
Dianne Louise Rhodes
WESTAT
An Employee-Owned Research Corporation
1650 Research Blvd.
Rockville, MD 20850

You might also like