Performing Conditional Processing Part B: Learning SAS by Example Questions Solved

Hi Folks!!

Welcome to the second part of the blog where we would aim at understanding how do conditional formatting helps in "making decisions" based on data values. We have earlier done 1 Question in Part 1 here we would be solving the other 5 Question given in the book Learning SAS by example.

Q1)
Lets see the code and try to understand the same:



Code:
 
 
 
 
 
The 1st snippet creates the hospital dataset in the library A15035. Our focus is to understand the use of in and or operator.

In and or operator helps in getting the same output. Let us understand after breaking the two codes

Proc print is used to print the observations in the A35_hosp dataset from A15035 library
where has been  used to put some constraints ordering the command to output the observations based on conditions specified after the where clause

Or statement is simply checking if the subject values are 5,100,150 and 200 then only print the observations however in command is the substitute of or which makes the code and programming more simpler you just specify variable in (range separated by spaces).

One can separate the ranges with comma or blanks or even :


Output:

 Understanding:
By using or and in operator we extract the subject which has a value of either 5,100,150,200
This example showed us the easy use of the operators or and in

Q)

Code:



Code Explanation:

1) We are creating a dataset A35_sales1 in the library A15035

2) The set statement can be thought as the input statement except you are reading observations from the SAS dataset instead of the lines from the raw data file.

3)Variables that are read from SAS dataset are not set to missing values during each iteration of the Data step . Here we are reading the data from sales dataset from library A15035

4)A select statement provides the alternative to the if else statement the expression following the select is select expression and expression following when is when expression . In this code select expression(region) is compared to each when expression . If the comparison is true then when expression is executed else observation following is executed.

5) In this case based on various regions North , South ,East and West the variable weight is created with values 1.5 , 1.7 ,2 ,2 respectively

6) Next we use proc print procedure to print the observations from A35_sales1 for variables region and weight

For better clarity we showed Region and Weight as output as per the question one can change the variable to totalsales as well


Output:
Q5-Q7:


Code:



We picked the last 3 questions together

Q5 Code Explanation:

We want to create the new variable Cholgroup and based on various specifications in attribute Chol we want to assign values to Chol. Lets look at the breakup of the code.

The use of select statement has been mentioned earlier .

1) we are creating the new dataset A35_blood1 in library A15035.

2) By using set function we are reading from the dataset A15035.blood

3) It checks if there is any missing value in chol attribute assign the value of blank to variable Cholgroup

4) In case it is less than 110 or 140 assign the value as Low and Medium respectively

5) Otherwise assign the value High

6) we use proc print to print the observations for the dataset A35_blood1



Q6 Code:

Lets break the code:

We can use the simple precedence rules for the subsetting of the data based on the conditions

1) we are creating the new dataset A35_sales2 in library A15035.

2) By using set function we are reading from the dataset A15035.sales

3) where command is used to specify the conditions here we have 2 brackets these are given more preference . 1st bracket checks if region is north and quantity is greater than 60 . Here both the conditions has to be true for the 1st bracket to be true. Next bracket is checking if the customer name is pets are ours.

4) Or checks whether the data which is read from A15035.sales is fulfilling either of the brackets conditions if yes it would print those observations otherwise ignore them.


Q7 Code:

1) we are creating the new dataset A35_bicycles1 in library A15035.

2) By using set function we are reading from the dataset A15035.Bicycles

3) where command is used to specify the conditions here we have 2 brackets these are given more preference . 1st bracket checks if Unit Cost is greater then 2500. Next bracket is checking if the model is hybrid and unit cost greater than 660. Here it will check both the conditions . hence then only the 2nd bracket would hold true.

4) Or checks whether the data which is read from A15035.bicycles1 is fulfilling either of the brackets conditions if yes it would print those observations otherwise ignore them.







Output: 

5:








6:
7:
 
Learning: 
 
Conditional Processing allow programs to “make decisions” based on data values.
 
For example, you may want to read a value of Age and create a variable that represents age groups. You may want  determine if values for a particular variable are within predefined limits.

Programs that perform any of these operations require conditional processing—the ability to make logical decisions based on data values.
 

 





Comments

Popular posts from this blog

Kabaddi Match: Lets meet at the arena!! Aa jao Dam Dikhane!!!

Text Analytics Using R - Part A: Extraction of reviews of galaxy s4 product reviews in flipkart

Replace your Social Media with the New Age Social Media: Fitness with Motivation