SAS: Bar Chart

Let’s create a bar chart in SAS. There are two ways you can go about it: Code or work with the menus provided. We will start with the menus.

Start by clicking on Task and Utilities

2016-11-28_21-30-01

Select Graph > Bar Chart

2016-11-28_21-30-45

In the new window pane that opens, we need to add Data. Select the browse button

2016-11-28_21-31-16

Expand SASHELP (a collection of data sets that came with SAS)

2016-11-28_21-31-41

We are going to pick SHOES

2016-11-28_21-33-02.jpg

Now the red * indicates required fields. The only one bar charts require is Category variable. Hit the plus sign.

2016-11-28_21-33-27.jpg

Select Product – notice how the code automatically fills in for you now.

2016-11-28_21-33-47

Hit the little running man and your chart will appear in the Results

2016-11-28_21-34-14.jpg

Let’s change the color. – go to Options>Bar Details>Apply bar Color and pick a new color

2016-11-28_21-34-44.jpg

And now it is red (or whatever color you chose)

2016-11-28_21-35-00.jpg

Go back to the Data tab and add Region to Group Variable

2016-11-28_21-35-25

Notice we now have two dimensions Product and Region. Also notice a legend is auto generated.

2016-11-28_21-35-41.jpg

Now let’s try doing via code, use the following code:

proc sgplot data=SASHELP.SHOES;
 vbar Product / group=Region groupdisplay=Stack name='Bar';
 yaxis grid;
run;

Note I changed groupdisplay to Stack. Here are the results

2016-11-28_21-37-21

 

SAS: Importing .txt .cvs and .xlsx files

SAS is a data based system. It is not much use if you can’t get data into it. In the following exercises, I will show you how to import common data files.

You can download some practice data files here: examplefiles

This is a zip file. Unzip the folder and copy the contents (three files) into a place of your choosing. Now open up SAS Studio:

Select MyFolders on the left pain and select the upload files icon

2016-11-27_17-06-05

Now select Choose Files

2016-11-27_17-09-08.jpg

Your files should now be located under My Folders

2016-11-27_17-10-52

Import Txt

If you double click on mileage.txt in the left panel, a preview of the data will pop up.

2016-11-27_17-12-46.jpg

Now to import this information into SAS, we use the following code.

DATA mileage;
INFILE '/folders/myfolders/mileage.txt';
INPUT year miles;
RUN;

DATA – names your data set

INFILE choose data source

INPUT – names variables(columns)

Now click the Run icon

2016-11-27_17-17-04

Now check your results, you data is loaded.

2016-11-27_17-31-20.jpg

A quick note, SAS chooses a single space as the default delimiter. If your file has a different delimiter like comma.

INFILE '/folders/myfolders/mileage.txt' DLM=",";

tab delimited
INFILE '/folders/myfolders/mileage.txt' DLM= '09'x

Import CSV

Now lets import our CSV file

DATA salaryHosp;
INFILE "/folders/myfolders/salaryHosp.csv" DSD Firstobs=2;
INPUT job$ years salary;
RUN;

DSD helps to deal with missing values

Firstobs = 2 lets SAS know to start collecting data on the second row – since the first row in our CSV file is headers.

2016-11-27_17-38-22.jpg

Import Excel

Importing Excel is pretty straight forward. Use the following code and SAS will take headers from the Excel file to name your variables.

proc import out=sensors DATAFILE="/folders/myfolders/sensors.xlsx" 
                   DBMS=XLSx;
RUN;

the results

2016-11-27_17-41-26.jpg

 

 

SAS: Introduction to SAS

SAS is a dominant force in the world of analytics. While some may argue it is on its way out, a quick query of current Data Scientist job listing in the NYC area shows many companies are still looking for SAS experience.

Luckily for us, SAS has a free University Edition that we can work with to learn SAS for free.

You can Google SAS University Edition or follow the link below:

http://www.sas.com/en_us/software/university-edition/download-software.html

First you will need to download and install VirtualBox, you can download it from the SAS page. They have a link.

2016-11-26_14-28-54

Next download the SAS  OVA package

2016-11-26_14-29-33.jpg

Once downloaded, just double click on the OVA file and it should automatically install itself into VirtualBox. Note – you must have VirtualBox installed first.

Now, before we can start, you have to make a shared folder on our computer. I just created the following in on my PC.  C:\SasUniversity\Myfolders

Now start VirtualBox: Go to SAS University Edition

2016-11-26_14-33-52.jpg

Right click and click Settings

2016-11-26_14-34-07.jpg

Now select Shared Folders – click the blue folder with the green plus sign on the right

2016-11-26_14-34-25.jpg

Type in the path of your folder, the name of your folder and check Auto-mount and Make Permanent

2016-11-26_14-39-14

Close the Setting window out and double click on  SAS University Edition to get it started.

Once it fully loads, you will get a screen like this

2016-11-26_14-40-26

Type the path given into a browser on your machine.

In my case the path is: http://localhost:10080

Once it opens, click Stat SAS Studio

2016-11-26_14-43-07.jpg

Here is SAS Studio. The Left panel of the screen manages folders and other admin tasks. The right panel is where we type our code. We are going to start with a very simple task of creating a small data set of car names, # of cylinders, and # of doors.

2016-11-26_14-45-03.jpg

Here is the code, I will explain  below the picture.

2016-11-26_14-54-54.jpg

Now to breakdown the code (note the ; (semicolon) at the end of each line)

DATA cars;  -- this names our data set
INPUT NAME$ CYLINDERS DOORS;  -- names our variables (or columns if you 
                                 will) note the $ after NAME - this 
                                 indicates NAME is
                                 a string
datalines;  -- the data to input into our set will follow below
               you may see some people use CARDS; in place of 
               datalines I believe this dates back to earlier  
              versions where one told how much memory to associate 
              to each row. Now they are interchangeable.
Miata 4 2
Sonata 4 4
Corvette 8 2
Mustang 8 2
Civic 4 2
Accord 4 4
Elentra 4 4
;                 -- end of datalines
RUN;              -- run command - like the go in SQL

 

Now click the Run icon above your code

2016-11-26_14-59-31.jpg

And view the output – we have created a data set – congrats!!

2016-11-26_14-59-55