Friday, 2 October 2015

SAS Assignment - Problems from Learming SAS by Examples by Ron Cody--- Chapter 16 - 20 ---- SID A15027

Please refer to the book "Learning SAS by Examples: A Programmer's Guide" by Ron Cody for better details.



In this blog we will look at few codes from chapter 16 to chapter 20. Here, we will briefly explain what functions and statements we are using and what type of output it will generate.

For codes and datasets, please click this here.

Lets start with Chapter 16

Chapter 16 : Summarizing your data

We have thought of PROC MEANS (or PROC SUMMARY) primarily as a way to generate summary reports, reporting the sums and means of your numeric variables. However, these procedures are much more versatile and can be used to create summary data sets that can then be analyzed with more DATA or PROC steps.

In the example we have used PROC MEANS rather than PROC SUMMARY, even when all we want is an output data set. The reason for this is that using PROC MEANS with a NOPRINT option is identical to using PROC SUMMARY.

16.1
Problem: Using the SAS data set College, compute the mean, median, minimum, and maximum and the number of both missing and non-missing values for the variables ClassRank and GPA. Report the statistics to two decimal places.

Code:
Output:

   

Code Explanation: In this code we have computed the mean, median, minimum, and maximum and the number of both missing and non-missing values for the variables ClassRank and GPA using the proc means statement. We have used n to find the frequency of the variables, nmiss to find the number of missing values, mean, median, min and max  to find the average, median, the highest and the lowest number and the numbers are rounded off to 2 decimal places using maxdec.



Chapter 17: Counting Frequencies

PROC FREQ can be used to count frequencies of both character and numeric variables, in one-way, two-way, and three-way tables. In addition, we can use PROC FREQ to create output data sets containing counts and percentages. We can also use this procedure to compute various statistics such as chi-square, odds ratio, and relative risk.

17.3
Problem: Using the SAS data set Blood, generate one-way frequencies for the variables Gender, BloodType, and AgeGroup. Use the appropriate options to omit the cumulative statistics and percentages.

Code:

Output:

Code Explanation: In this code we have generated one-way frequencies for the variables Gender, BloodType, and AgeGroup. First, we have built a format to built a cholestrol group e.g. if low-200, it will display Normal. If 201-high, it will display High and Missing for ".".

In the first proc freq statement, when we haven't mentioned  missing in the tables statement, it displays only the frequencies of Normal and High Cholesterol level and their respective percentage.

But in the second proc statement, on introducing missing in tables statement, an additional column is displayed in the table.


Chapter 18: Creating Tabular Reports

PROC TABULATE is an underused, under-appreciated procedure that can create a wide variety of tabular reports, displaying frequencies, percentages, and descriptive statistics (such as sums and means) broken down by one or more CLASS variables. 

18.5
Problem: Produce the following table. Note that the keywords ALL, N, MIN, and MAX have all been renamed.

Code:

Output:

Code Explanation: In the above code, we have used proc tabulate statement to build the table as asked in the question. Here we have classified the columns in Male and Female columns using the class statement. Then we have calculated the minimum, maximum, average of the GPA and also counted the number of students under male and female. We have renamed the rows using keylabels statement.


Chapter 19: Introducing the Output Delivery System 

Output Delivery System is abbreviated as ODS. Each SAS procedure creates output objects that can be sent to such destinations as HTML, RTF, PDF, and SAS data sets. There is now a separation between the results produced by each procedure and the delivery of this information. Not only we can send our SAS output to all of these formats, we can also customize the output’s fonts, colors, size, and layout. We can also capture virtually every piece of SAS output to a SAS data set for further processing.


19.3
Problem: Run the following program, sending the output to an HTML file. Issue the appropriate commands to prevent SAS from creating a listing file. See Chapter 16, Problem 2, for the note about creating formats for this data set.
title "Sending Output to an HTML File";
proc print data=learn.college(obs=8) noobs;
run;
proc means data=learn.college n mean maxdec=2;
var GPA ClassRank;
run;
Run the same two procedures as shown in Problem, except use the JOURNAL (or FANCYPRINTER) style instead of the default style.

Code:













Output:


Code Explanation: In the code given in the problem statement, we style=journal in the ods html statement to change the display of the output. Thus, using the style statement, we can create different designs of the output as per our requirement. 


Chapter 20: Generating High-Quality Graphs

Here, we will mostly learn about the application of various charts and how to implement it on SAS. We will mainly focus on gchart procedure with which we can create horizontal, vertical and pie charts.

20.3
Problem: Using the SAS data set Bicycles, produce a vertical bar chart showing the distribution of Total Sales (TotalSales). Use midpoints of 0 to 12,000, with intervals of 2,000.

Code:









Output:


Code Explanation: In the above code, we have created a bar chart to show the distribution of sales. Here, we have used pattern value=solid statement to create solid bars. gchart procedure is  used to create bar and pie charts. vbar statement is used to create vertical bar charts and hbar is used to create horizontal bar charts. midpoints <value1> to <value2> by <value3> is used to create bar charts of equal class distance of value 3 between value1 and value2.