Saturday, 19 September 2015

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

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

In this blog we will look at some codes. Here, we will briefly explain what functions and statements we are using and what type of output it will generate.

To get the codes, please click here.

We will start from Chapter 7

Chapter 7 : Performing Conditional Processing


7.4


Output:


Solution Explanation: Here we are using when statement to select observations based on their regions and we have created a new column called weight. We are also assigning weights for the respective regions. Here we have shown the use of drop statement to keep on the variables required for the program.

7.6



Output:

Solution Explanation: In this code, we have shown the use of WHEN statement and also the relational operator like AND and CONDITIONAL operator like less than(<). In this program, we are displaying the observations where region is "North", quantity is less than 60 and customer is "Pet's are us".





Chapter 8: Performing Iterative Processing: Looping


8.2



Output:


Solution Explanation: Here we have written the code to perform cumulative summation of sales from the first month to the last. Here we have assigned total as 0 at the first month. As the loop progresses the sales of each month gets added to the total. Here we have also used @@ operator, where the pdv reads till the end of the line.

8.4


Output:


Solution Explanation: In this code, we are finding the missing value in any column. If there is any missing value, the output will be 1 else the output will be 0. Here, we have used the MISSING() function to detect any missing value.


Chapter 9: Working with Dates



9.6






Solution Explanation: In this code we have used the Medical data set to compute frequencies for the days of the week for the date of the visit (VisitDate) using PROC FREQ. Here we have used weekday() and month() functions to extract the day no. and the month no. from the visitdate. Then we have used format step to provide proper names to the days and the months.


9.8



Output:



Solution Explanation: In this code, we are converting the variables collected from the datalines into a proper date using the mdy() function. The mdy() takes three variables and converts them in date. then we have used the format option to convert the date into date9 format.

Chapter 10 Subsetting and Combining SAS Data Sets


10.1



Output:


Solution Explanation: We have created two temporary SAS data sets called Subset_A and Subset_B. Here we have included in both of these data sets a variable called Combined equal to .001 times WBC plus RBC. Subset_A which consists of observations from Blood where Gender is equal to Female and BloodType is equal to AB. Subset_B consists of all observations from Blood where Gender is equal to Female, BloodType is equal to AB, and Combined is greater than or equal to 14. Here we have used WHERE and LOGICAL and CONDITIONAL operators. We have also used SET statement to load a dataset and create new datasets from it based on the requirements.

10.4


Output:


Solution Explanation: Using the SAS data set Bicycles,we have created two SAS data sets as follows: Mountain_USA consists of all observations from Bicycles where Country is USA and Model is Mountain. Road_France consists of all observations from Bicycles where Country is France and Model is Road Bike. Here we have used set statement to select a dataset from which we have selected observations and created new datasets as per our requirements. Here, we have used OUTPUT statement to select the observations of certain conditions and put it in a selected dataset.

Chapter 11: Working with Numeric Functions


11.1



 Output:

Solution Explanation: Here we have created 4 variables for bmi report.

BMI=weight/height

Here we have tried to show the use of round and int function in SAS to round off a number to certain decimal places as per our ease.

11.2

Output:

Solution Explanation:  Here we have used the missing function in the blood dataset to find if their is any missing values present in RBC, WBC and Chol. If present, it is depicted by 1 and if not, 0 is displayed.

Chapter 12: Working With Character Functions

12.2




Solution Explanation: Here we have used the function lowcase and propcase to convert a string into lower case and proper case.
We have also shown how to convert a string into proper case without using the propcase function. To achieve that, we have used upcase, lowcase, trim and catx functions.

12.4

Output:


Solution Explanation: In this code, we have converted the height which is a string into a numeric variable, where the height is displayed in inches. To attain so, we have used compress function along with kd which removes any characters and space present in string. Then we have used input function to convert a string to a variable.

Chapter 13: Working with Arrays

13.2


Output:


Solution Explanation: Here, we have tried to show how we can use an array where the values of the variables Ques1–Ques5 are stored in reversed order.

13.3


Output:

Solution Explanation: In this code, we will use an array and replace with a SAS missing value(.)  where the value is 999.
We will also use _Numeric_ to define the array as numeric.

Chapter 14: Displaying your Data


14.2



Output:


Solution Explanation: Here we will the dataset sales, where we want to display only region, quality and total sales columns, where region is "EAST" using VAR and WHERE statements and also find the total sum of the columns quantity and totalsales withe the use of sum.

14.4
Output:


Solution Explanation: Here we want to print the observations of the columns subject, gender and bloodtype in the same order. We have done this using the VAR statement.
If we want to remove the observation table, we can always use NOOBS statement in the PROC PRINT statement.


Chapter 15 : Creating Customized Reports


15.1


Output:


Solution Explanation: Here we have shown the use of PROC REPORT procedure. In this code, we have used the data set Blood. Here the variables we listed are Subject, WBC, and RBC.

15.3

Output:


Solution Explanation: Here we are using the Hosp data set. We are creating the report. We use the COLUMN statement to set the desired variables for which we need the report. We use define statement to display the column name as per our wish. This define statement does not change the variable name though. We are also using a compute statement through which we can add another temporary variable whose value is calculated in the PROC REPORT statement. We have used the YRDIF function to calculate the year difference between the two variables DOB and ADMITDATE.

2 comments:

  1. You have a lot to improve on the blog. Think of this exercise as making your class notes and homework copy, combined.

    The blog would look much better, if you:

    1. Make separate page for each chapter and have an index page, linking the pages (for the chapters).  Add links at each chapter to go back to index, previous (if any) and next (if any) pages.
    2. Introduce the chapter (i.e. the page) in your own words.
    3. For each problem, explain the problem you are attempting to solve. Cut and paste from Cody's book should be OK.
    4. Put screenshots of programs and results. Crop them to the required porting only. Otherwise you will have a lot whitespace in your blog.
    5. For each problem solved, write what you learned by solving the problem.  Even one line is fine for this. 
    6. Write a conclusion or summary for each page.

    Remember to :
    1. Number the problems solved by giving Chapter # and Problem # (as you did)
    2. "Beautify" the programs before posting.  There a menu item in SAS studio, which does the beautification for you.  Also, you may like to post the programs, the way you have posted the result.

    I understand, it is lot of work now, given you are trying to do it at eleventh hour.  For the next submission, if time permits, you can give it a try.

    ReplyDelete
    Replies
    1. Thanks for your feedback.

      As per your instructions, I have made necessary changes in the blog. Kindly check the same and share your thoughts on it.

      Delete