Reference no: EM133147815 , Length: word count:800
ITECH3101 Business Analytics and Decision Support - Federation University
Exercise - Introduction of SAS programming
Project 1: Knowing DATA steps and PROC steps
SAS programs consist of DATA steps and PROC steps.
In DATA steps, you can create data set and read, write, and manipulate data. Then DATA steps pass the data to a PROC step for processing. DATA steps begin with keyword DATA and usually end with a RUN statement.
In PROC( short for procedure) steps, you can use built-in procedures to analyze data and complete tasks such as writing reports, summarizing data or creating graphical output. PROC steps begin with the word PROC and end with either a RUN or QUIT statement(or both).
Task: Write your first SAS program.
Step 1. Get into your SAS Studio, and select CODE tap Step 2. Write code and run program.
Step 3. Look at LOG and RESULT. Step 4 Store your program
Step 1. Get into your SAS Studio, and select CODE tap.
When you open SAS Studio, make sure that you have CODE highlighted. The CODE window is a place where you write your SAS program.
Step 2. Write code and run program.
(1) You may first write the comment with an asterisk and end it with a semicolon. The comment will introduce the purpose of the program.
(2) Select Preferences to Enable auto complete function
When you type some keywords in the CODE window, SAS Studio shows you syntax and options for helping program. You can select or deselect Autocomplete function.
(3) Write code and run the program
In this program:
Statement 2 names a data set called Convert.
Statement 3 tells the program where original data is located. To read and write data between SAS Studio session and your hard drive, you need to place your data in a specific location: \SASUniversityEdition\myfolders.
You can place \SASUniversityEdition\myfloders at location you selected. This location (\SASUniversityEdition\myfolders) is mapped to a shared folder called
/folders/myfolders in SAS Studio.
Statement 4 reads data from the celsius.txt file.
Statement 5 uses a formula to convert Celsius to Fahrenheit. Statement 6 ends DATA step.
Statement 8 lists the content of data set convert.
Statement 9 specifies which variables you want to include in report.
Step 3. Look at LOG and RESULT.
After running your program, you can have a look at the result of program.
The RESULTS window is place where SAS output appears. Click RESULTS tap:
The LOG window displays your program, any syntax errors in the program, information about data reading and writing and CPU time used, etc.
Step 4 Save your program
From CODE tab, select the icon for SAVE AS
SASUSER is a permanent SAS library that is created automatically for each user. This library should have been allocated to the shared folder:
\SASUniversityEdition\myfolders\sasuser.v94.
After selecting the Save As and expanding My folders, select assuser.v94 and enter program name, and then click on Save button.
Summary of storage of SAS data and program files
A SAS library is a location where SAS data sets and other types of SAS files are stored.
1) SASUSER is a permanent SAS library that is created automatically for each user.
2) WORK library is a temporary storage location for SAS data sets. It is the default library. If you create a SAS data set without specifying a library, SAS will put it in the WORK library, and then delete it when you end your session.
3) Create your new library
Click New Library tap to open new library window.
In the New library window, type the name of library. In Path field, click the Browse button to navigate to the folder or directory where you want your data sets to be stored.
Then click Ok and the library Week2 you just created will appear in Active Libraries window.
Project 2: Basic programming rules
Task: This project will focus primarily on basic programming rules of SAS, data step part of SAS and proc step part of SAS.
1. Basic Concepts and Rules
1) Each statement in SAS must end in a semicolon (;).
2) SAS variable names must be 32 characters or less, constructed of letters, digits and the underscore character.
3) SAS is not case sensitive, except inside of quoted strings
4) Data set names follow similar rules as variables, but they have a different name space
5) Missing values are handled consistently in SAS, and are represented by a period (.).
6) Names must start with a letter or an underscore ( _ ).
7) Data type. There are two data types: numerical and character.
2. SAS programs consist of two types of steps
1) DATA (data step):
• Begin with DATA statements
• Read external (or internal) data files to create SAS datasets
• Create new SAS datasets by submerging, merging, modifying, and updating existing SAS data sets.
• Create new variables
• Analyze, operate or display data
• Extract the information
• Generate reports or save files to hard disks
Please note that DATA steps execute line by line and observation by observation.
(1) Execute line by line
SAS executes line one of your DATA step before it executes line two, and line two before line three, and so on.
(2) Execute observation by observation
SAS takes the firs observation and runs it all the way through the DATA step
(line by line) before looping back to pick up the second observation. In this case, SAS sees only one observation at a time.
DATA steps execute line by line and observation by observation
2) PROC (procedure step):
PROC STEPS invoke and execute programs through a series of SAS statements. Usually a data set is used as input data.
(1) begin with PROC statements
(2) perform specific analysis or function such as produce summaries of the data
(3) produce results or report such as tables, plots and results of statistical analyses
SAS contains a vast number of procedure that has been developed over many years and by many teams of programmers.
All procedure steps begin with PROC. Overall procedure syntax:
PROC procname DATA=dataname; (specific part of program)
RUN;
Please note that always start by naming the relevant procedure and the data to be analyzed.
Project 3: Example of DATA steps and PROC steps
Task: Make data in SAS.
Step 1. Get into your SAS Studio, and select CODE tap Step 2. Write code and run program.
Step 3. Look at LOG and RESULT. Step 4 Store your program
Step 1. Get into your SAS Studio, and select New icon on the left-hand pane under the Server Files and Folders.
Step 2. Write code and run program.
After clicking Run tap, we get
Question 1
In DATA step in the above program, there are two names. Why is there only one row of data instead of two rows in OUTUT DATA tab?
The reason is that the code did not explicitly output the second set of variables so the second set of data is not written out and saved into the dataset.
Step 2. Add the second output before the run
After clicking Run tap, we get
Question 2
In OUTUT DATA tab, the second value of name should have been Steve, but it gets truncated to Stev. Why is the second value for the name variable truncated in OUTUT DATA tab?
The reason is that SAS implicitly declared name to be a character based on the First instance ( Mike) and it also implicitly declared the length of character variable based on the value it had information on (Mike). Therefore, the length was implicitly set to 4. In this case, Steve gets truncated to Stev.
Step 3. Use length data step statement
There is a dollar sign ($) after the variable name. It indicates that name is declared to be a character and its length is 5.
After clicking Run tap, we get
Step 3. Explicitly declare the types and lengths of variables
There is a better way to make data within SAS. For example, we can explicitly declare the types and lengths of variables. In addition, we can use INPUT and DATALINES or CARDS data step statements to read in multiple rows of data all at once.
After clicking Run tap, we get
• length statement explicitly declares the types and lengths of variable: name is a character variable and its length is 5. x and y are numeric variables and their length are 8.
• input statement tells SAS how to read raw data. In this case, this statement tells SAS to read three data values from a character variable name and 2 numeric variables x and y.
• datalines ( or cards) statement is used to read raw data directly in your SAS program and it must be the last statement in the DATA step.
Project 4: Making datasets using SQL type statements
1. We can use PROC SQL to make datasets within SAS. There are two basic ways to use SQL with SAS;
(1) embed complete SQL statements in the SQL procedure
(2) use WHERE statements or WHERE= data set option to select rows in SAS DATA and PROC steps
2. Basic form of SQL procedure is:
PROC SQL;
sql-statement;
The possible values for sql-statement in PROC SQL include CREATE, DELETE SELECT, UPDATE, etc, with a semicolon stuck on the end.
3. SQL does not contain structures like SAS DATA and PROCE steps and operations performed by most other SAS procedures don't exist in SQL.
4. In SQL, reports are written automatically whenever you use a SELECT statement.
5. PROC SQL can run interactively without a RUN statement.
Examples:
1) making a new dataset
In the above code, you make a new dataset named sqltry1 in work library with three variables and no observations or rows.
After clicking Run tap, we get
2) You can also use PROC SQL to create new datasets through querying a table that already exists within an existing SAS library.
After clicking RESULTS tap, we get
Project 5. Answering questions (Please do this at your home by using your own computer)
1. List five best practices of dashboard design
Project 6. Creating a Professional Report
Summarize the above experiments procedure, results, answering questions and screenshots (project 1, 2, 3, 4, 5) 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 5 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 5: Introduction of SAS programming
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:- Introduction of SAS programming.rar