This is a simple tutorial showing how to use SQL to subset data, and then create a histogram using Proc Sgplot, in SAS University Edition.

So you've downloaded SAS University Edition, and you're wondering "What now?" -- I would recommend exploring some of the sample data, and creating some simple charts!

To explore the sample data, select 'Libraries' along the left side, and then expand the 'SASHELP' library. This will show you a list of all the sample data that is included with the SAS University Edition. Scroll through the list of datasets, and look for names that might interest you.

For this example, we'll be using the SASHELP.HEART dataset, which contains some heart-related data about several patients. Double-click SASHELP.HEART, and it will let you browse the data in a spreadsheet-like interface. Scroll left/right in the data, and notice there is a column for Sex and columns for Diastolic, and Systolic blood pressure (these are the values we'll be using in our graph).

We could easily plot all the data, but it is very useful to know how to plot just a subset. There are several ways to subset data in SAS, but I'm going to teach you how to do it with Proc SQL ... because SQL is a very versatile tool to use, and also because many of you might already be familiar with SQL if you've worked with databases.

The following SAS SQL code will create a new dataset called male_data, containing ... you guessed it! ... just the data for the males. Type this code into the CODE tab of the Program 1 window, and then click the Run button (icon of a little man running). Yeah, I know - I'm a meanie, making you type it in, rather than copy-n-paste -- but this is part of the learning process! :)

Did your code run smoothly? If not, check to make sure you have a matching single-quote on both sides of 'Male', and make sure you have all four semicolons! Once you've got it running smoothly, then you can add the following Proc Sgplot code to create a histogram:

Double- and triple-check to make sure you have all the code typed in correctly, with all the quotes, slashes, and semicolons ... and then click the Run button. If you've done everything correctly, you should get a chart like the following:

So now you know how to view the sample data, manipulate it with SQL, and create a simple chart - you're well on your way to becoming a highly-paid SAS programmer! :)

Share

The Graph Guy!

Robert has worked at SAS for over a quarter century, and his specialty is customizing graphs and maps - adding those little extra touches that help them answer your questions at a glance. His educational background is in Computer Science, and he holds a BS, MS, and PhD from NC State University.