Learning by doing – Simple SAS codes (Part II)

Welcome to the second post of the series Learning by Doing – Simple SAS codes.

Recap

Lets recap what all we have learned in the last post. The main topics which we covered in last post (Click here to open) are as follows:-

  1. if-else logic
  2. select-when-otherwise-end statement
  3. missing function
  4. do loop
  5. date functions like yrdif
  6. multiple datasets in single data step
  7. _NULL_, Constant, exactint, Put_ALL_
  8. find,input,compress and round functions
  9. proc sort, by function, where condition
  10. proc report- define, compute

Intro

In this post we are going to look at the following chapters:-

  • Summarizing data
  • Counting frequencies
  • Creating tabular reports
  • Output delivery systems
  • Generating high quality graphics

Chapter-16 – Summarizing your data

In this chapter we will learn how to summarize data with the help of procedure called proc means and its various functions like by, class, _TYPE_ etc.

Lets solve few problems in these chapters.

2nd question from Ron Cody.

We have data of college students, now we have to find number of students, how many students data is missing, minimum value of each variable, maximum, median for two variables mainly ClassRank and GPA using BY function and Class function.

We’ll be using the same directory A15004 which we used for previous post (Click here to open)

Code for the program is:-

Code

You can download code by clicking on this link

The first proc step of this code will sort the data according to gender and schoolsize. Second and third proc steps are two different ways to summarize data one is by ‘BY’ function and second is by ‘Class’ function. The respective outputs are pasted below.

Output1

Output for 2nd method of doing same is as follows.

Output2

Learning in this chapter

Learned various types of ways to summarize data using PROC Means, first we summarized using by function and second we have done using a class function.

Chapter 17: Counting frequencies

In this chapter we will learn how to use proc freq in detail.

Lets take a problem from Ron Cody and apply our understanding using SAS tool.

In this chapter we will use various procedures like Proc freq, Proc sort & Proc format. Lets create a three way table of college dataset.

This is the code which we will use for doing the same.

Code

You can download code from this link.

Now lets look at the output you can see the perfectly sorted frequency table – 3 way table of variables Gender, Scholarship, Schoolsize. I have deliberately removed percent and cumulative percentages in the respective columns.

17.2

Learning from this chapter

We have learned procedure proc freq in this chapter and its functions. learned how to create single table, two way table and three way tables which was our program in this chapter.

Chapter-18: Creating Tabular reports

In this chapter we are going to learn about Procedure called PROC TABULATE and its functions.

Lets take a task from Ron Cody to create a table and calculate the row percentages of the respective table. The dataset used in this example is college dataset.

Here is the code used to create tabular report.

Code

You can download the above code from here.

The output for above code is as follows.

18.2

Learning in this chapter

In this chapter we have learned how to use procedure proc tabulate and its functions and various options.

Chapter-19: Output Delivery Systems

In this chapter we will learn how to save our output in html format. We will use functions like ods html etc.

Lets take an example and understand this further.

As we are working with college data set. Lets continue with it. Now our task is to write our output into a html file.

Here is the code for the same

Code

You can download the code for the above program here.

The output from the above code will create a html file in the assigned library in our case it is A15004 and also produce following output.

19.2

Learning in this chapter

We have learned how to store our output as a html file in this chapter and most importantly we learned how to use various functions of odc html functions along with other procedures like proc means in this case.

Chapter-20: Generating High quality graphics

As we are using SAS university edition for demonstration of the codes and functions there shall be a slight change in the procedure used in the functions instead of proc gchart and ggplot we shall use proc sgplot which is almost similar to other procedures.

Lets take a problem from ron cody and see some of the functions to create graphs.

First lets create pie chart and use the blood dataset. Here is the code to create pie chart.

20.1 code

You can download the code from here.

And the output from the above code is as follows:-

20.1

So we have now created pie chart. And used blood dataset with Chol variable.

Now lets create barchart using bicycles dataset.

20.2

You can download the code from here.

The output of the above code is as follows

Barchart

Learning in this chapter

In this chapter we have learned how to create pie chart and bar charts and also to change colors of bars and to add data labels to them as well.

So this completes the series of posts that we are learning by doing with simple sas codes.

Cheers!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s