Reference no: EM133147843 , Length: word count:800
ITECH3101 Business Analytics and Decision Support - Federation University
Exercise - SAS programming -2 Conditional and looping processing
Project 1: Conditional processing
All programming languages allow us to perform conditional processing that makes decisions based on data values or other conditions. We can perform conditional processing logic with IF-THEN statement.
1. IF-THEN statement
IF-THEN statement takes form of:
IF condition THEN action;
The condition is an expression comparing one thing to another, and the action is what SAS should do when the expression is true. for example
IF Model = 'Berlinetta' THEN Make = 'Ferrari';
This statement tells SAS to set the variable Make equal to Ferrari whenever the variable Model equals Berlinetta. The basic comparison operators are as follow:
Symbolic
|
Mnemonic
|
Meaning
|
=
|
EQ
|
equals
|
^= or ~=
|
NE
|
not equal
|
>
|
GT
|
greater than
|
<
|
LT
|
less than
|
>=
|
GE
|
greater than or equal
|
<=
|
LE
|
less than or equal
|
The IN operator compares the value of a variable to a list of values. IF Model IN ('Model T, 'Model A') THEN Make = 'Ford';
This statement tells SAS to set the variable Make equal to Ford whenever the value of variable Model is Model T or Model A.
2. IF-THEN statement plus keywords DO and END
A single IF-THEN statement can only have one action. If we add the keywords DO and END, then we can execute more than one action.
IF condition THEN DO; IF Model = 'DMC-12' THEN DO;
action; Make = 'Delorean';
action; BodyStyle = 'coupe';
END; END;
The DO statement causes all SAS statements coming after it to be treated as a unit until a matching END statement appears.
AND and OR comparison operators
Symbolic
|
Mnemonic
|
Meaning
|
&
|
AND
|
All comparisons must be
true
|
|, or !
|
OR
|
Only one comparison must
be true
|
Examples
Download Dataset: Auction.dat from Week 8 in Moodle and save it under myfolders on your computer.
In above code, the first IF-THEN creates a new variable named Veteran and gives it a value of Yes for any car made before 1890. The second IF-THEN uses DO and END to fill in missing data for the model F-88.
After clicking Run tap, we get:
3. IF-THEN /ELSE statement
IF-THEN statement can be used to group observations. The simplest way to create a grouping variable is with a series of IF-THEN statements. By adding the keyword ELSE to IF statements, we can tell SAS that these statements are related.
IF-THEN/ELSE logic takes form of IF condition THEN action;
ELSE IF condition THEN action; ELSE IF condition THEN action;
Examples
Download Dataset: Home.dat from Week 8 in Moodle and save it under myfolders on your computer.
In above code, the first statement deals with observations that have missing data for the variable Cost. SAS considers missing values to be smaller than non-missing values, smaller than any printable character for character variables, and smaller than negative numbers for numeric variables.
So failing to account for missing values in a DATA step is the most common logic error in SAS programming.
The last ELSE statement contains just an action, with no IF or THEN. This kind of ELSE becomes a default which is automatically executed for all observations failing to satisfy any of the previous IF statements. We can only have one of these statements.
Project 2: Looping processing
Many programs use looping processing that executes a series of statements a fixed number of times or sets a condition causing the loop to start or stop.
1. Do Loops
SAS iterative statements start with the keyword DO and are called as DO loops. The general form of a DO loop is
do variable = lower-limit to upper-limit by increment; SAS statements;
end;
Usually SAS writes an observation to a data set at the end of the DATA step, but we can override this default using the OUTPUT statement. If we want to write several observations for each pass through the DATA step, we can put an OUTPUT statement in a DO loop.
Example 1
In above code, DO loop has six iterations. An observation is created each time through the loop because OUTPUT statement is inside the DO loop. Without the OUTPUT statement, SAS would have written only one observation at the end of DATA step.
After clicking Run tap, we get:
Example 2
In above code, data in the datalines expresses heart rates for subjects given one of three drugs: Placebo, Drug A and Drug B. Each line of data contains three heart rates, one for each of three drug group.
The trailing @ sign("hold the line") allows the program to read all three values from one line of data. The trailing @ sign on the input statement prevents SAS from going to a new line each time the loop iterates and INPUT statement is executed. noobs in proc print statement would remove the Obs from the output of proc print.
After clicking Run tap, we get:
2. DO WHILE loop
The syntax of DO WHILE loop is do while (expression);
SAS statements;
end;
where expression is a SAS logic expression. Example
After clicking Run tap, we get:
3. DO UNTIL loop
A DO UNTIL loop will continue to loop until the UNTIL condition is true. The UNTIL condition is evaluated at the bottom of the loop. Therefore, DO UNTIL loops will always execute at least one time.
Example
After clicking Run tap, we get:
Project 3. Answering questions (Please do this at your home by using your own computer)
1. Name and briefly describe four of the most popular commercial software tools used for text mining.
2. In application Case 7.3: Mining for Lies, how can text/data mining be used to detect deception in text?
3. You have data from three groups of subjects (Groups A, B and C). The actual data looks like this:
Group Score
A 10
B 11
C 12
A 20
B 21
C 22
However, the data was entered without the group, like this:
Data for Group Study 10
11
12
20
21
22
Write a program to read the six numbers shown here(use DATALINES) to create a table with the variables Group and Score.
Project 4. Creating a Professional Report
Summarize the above experiments procedure, results, answering questions and screenshots (project 1, 2, 3) into one report. Your report is the assignment that is required to be submitted for evaluation on week 11. Create a report by following below steps.
You can add a chapter called Chapter 8 in your previous report.
1. Open your last week's report and find the end of last week's report.
2. Copy this week's related experimental results, your findings and Screenshots, and paste them at the end of last week's report.
3. Delete original Table of Content you created.
4. Select all content , align all text to both left and right margin
5. Use shortcut key approach, generate Chapter 8: SAS programming -2 Conditional and looping processing
6. Then use shortcut key approach to generate proper sub-chapters for this week's lab work.
7. Insert Table of Contents to your report.
Attachment:- Business Analytics and Decision Support.rar