I Hate SQL! Tips and Tricks For Using The Query Window: Dianne Louise Rhodes, Westat, Rockville, MD
I Hate SQL! Tips and Tricks For Using The Query Window: Dianne Louise Rhodes, Westat, Rockville, MD
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 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.
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;
TRADEMARKS
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
REFERENCES
ACKNOWLEDGMENTS
CONTACT INFORMATION