Hello, dear friend, you can consult us at any timeif you have any questions, add WeChat: daixieit


Practicals: An Introduction to SAS Programming

Practical 1: Exploring SAS for the 1st time

SAS is a suite of business solutions and technologies to help organizations solve business problems. Base SAS is the centerpiece of all SAS software.

It can be useful to look at SAS capabilities in a simple framework:

• Access data: Using SAS, you can read any kind of data.
• Manage data: SAS gives you excellent data management capabilities
• Analyze data: For statistical analysis, SAS is the gold standard.
• Present data: You can use SAS to present your data meaningfully.

Understanding the SAS Programming Process

Here is the overall process of programming in SAS.

1. Define the business need.
2. Write a SAS program based on the desired output, the necessary input, and the required processing.
3. Run the program.
4. Review your results.
5. If you find inaccuracies or errors, you debug or modify the program.

Depending on your results, you might need to repeat some of the steps.

SAS can be used to read in almost any type of data, including the following three major file types:


  • Raw data files contain data that has not been processed by any other computer program. They are text files that contain one record per line, and the record typically contains multiple fields. Raw data files aren’t reports; they are unformatted text.
  • SAS data sets are specific to SAS. A SAS data set is data in a form that SAS can understand. Like raw data files, SAS data sets contain data. But in SAS data sets, the data is created only by SAS and can be read only by SAS.
  • SAS program files contain SAS programming code. These instructions tell SAS how to process your data and what output to create. You can save and reuse SAS program files.

1 Getting Started


Part 1: Create a folder for your practice files and define the PRAC library.

1. Download the folder PRACDATA to a location on your computer or network where you might typically store SAS files.

2. Start SAS Online.
3. On the sidebar click the starred piece of paper to create a new folder under “Files (Home)” Your user number will be different, but call the folder PRAC.

4. Right click on your new PRAC folder and select Upload Files… 

5. Click the Choose Files button, find and select all the practical datasets that you found on stream under Resources.

6. Right click on the PRAC folder once the files finish up loading and you can select Create -> Library
7. Be sure to tick the “autostart” clickbox so this data is always available to you.
8. Check if it created correctly by looking under the library section of the sidebar.You have defined the PRAC library and SAS knows where you are storing your practice files. A library is just a pointer to a folder containing your data (we’ll explain this in greater depth in the 2nd practical).

If the program does not run successfully, make sure you created the folder that you are referencing, and review your program for errors. Correct the errors and resubmit the program.

Finally, redo these steps to create a LAB folder and Library using the Lab Datasets found on stream.

Working with SAS Programs

Exploring SAS Programs

Generally speaking, a SAS program is a sequence of steps that you submit to SAS for execution. Each step in the program performs a specific task. SAS programmes are comprised of a sequence of steps, and a step is comprised of a sequence of statements. Every step has a beginning and ending boundary. These are called step boundaries. SAS compiles and executes each step independently based on the step boundaries.

Only two kinds of steps make up SAS programs: DATA steps and PROC steps.

A DATA step begins with a DATA statement. This reads data from an input source, processes it, and creates a SAS data set (which is data in a form that SAS understands). In addition, you can use a DATA step to create new variables that were not in your original data.

A PROC (or procedure) step begins with a PROC statement. This processes a SAS data set. Various PROC steps generate reports and graphs, manage data, and sort data.

One way to use these two steps together is to use a DATA step to create a SAS data set, and then use a PROC step to create a report.

SAS detects the end of a step when it encounters one of the following:

• a RUN statement for most steps,
• a QUIT statement for some procedures, or
• the beginning of another step.
SAS Programming
1. Copy and paste the following SAS programme into the editor:
data work.newsalesemps;
set PRAC.sales;
where Country='AU';
run;
title 'New Sales Employees';
proc print data=work.newsalesemps;
run;
proc means data=work.newsalesemps;
class Job_Title;
var Salary;
run;

title;

This program contains three steps: one DATA step and two PROC steps.

2. Submit the code (either by pressing F8 or the button [on the Application Toolbar]) and check the log.

It's a good programming practice to first check the log, even if the program appears to produce results (you want to ensure that the code ran successfully before you look at any reports SAS created). SAS should process the code without warnings or errors.

3. View the results. 

The first report is the PROC PRINT report. Recall that this type of report simply lists your data. You can see columns for the various variables and all of their values. The specified title appears at the top of the report. The next report is the PROC MEANS report. The MEANS procedure provides data summarization tools to compute descriptive statistics on your data, and displays output by default. Here, SAS calculated statistics for the analysis variable Salary.

Understanding the programme:
• In the 1st line of, the DATA step creates a temporary SAS data set named work.newsalesemps by reading the PRAC.sales data set.
• In the 8th line, the PROC PRINT step creates a list report of the work.newsalesemps data set.
• In line 11, the PROC MEANS step creates a summary report ofwork.newsalesemps with statistics for the variable Salary for each value of Job_Title.
• This SAS program also contains global statements.
• These statements can lie outside DATA and PROC steps, and they can affect more than one step. For example, the first TITLE statement located before the PROC statements, specifies a title that appears on both reports. The second TITLE statement located at the end of the SAS program, turns all titles off for all subsequent output.
Characteristics of SAS Programs

SAS statements usually begin with an identifying keyword, and they always end with a semicolon.

Keywords identify the type of statement, and semicolons end the statement.
For example, in the following SAS program, the second statement is a SET statement, and the fourth statement is a RUN statement.
data work.newsalesemps;
set PRAC.sales;
where Country='AU';
run;

SAS statements are free format and can begin and end in any column. A single statement can span multiple lines, and there can be more than one statement per line. Unquoted values can be lowercase, uppercase, or mixed case. This flexibility can result in programs that are difficult to read.

Conventional formatting, also called structured formatting, uses consistent spacing to make a SAS program easy to read. To follow best practices, begin each statement on a new line, indent statements within each step, and indent subsequent lines in a multi-line statement.

Using SAS Comments

Comments are used to document a program and to mark SAS code as non-executing text. There are two types of comments: block comments and comment statements.

4. Try running the following programme which uses a block comment :
/* create a temporary data
set, newsalesemps, from
the data set PRAC.sales */
data work.newsalesemps;
set PRAC.sales;
where Country='AU';
run;
proc print data=work.newsalesemps;
run;
5. Equivalently, a comment statement could be used . This begins with an asterisk, followed by the comment text, and ends with a semicolon.
*create a temporary data set,
newsalesemps, from the data set
PRAC.sales;
data work.newsalesemps;
set PRAC.sales;
*where Country='AU';
run;
proc print data=work.newsalesemps;
run;

Diagnosing and Correcting Syntax Errors

Syntax errors occur when program statements do not conform to the rules of the SAS language. Common syntax errors include misspelled keywords, missing semicolons, and invalid options. SAS finds syntax errors during the compilation phase, before it executes the program. When SAS encounters a syntax error, it writes the following to the log: the word ERROR or WARNING, the location of the error, and an explanation of the error. You should always check the log, even if the program produces output.

Mismatched or unbalanced quotation marks are considered a syntax error. In some programming environments, this results in a simple error message. In other environments, it is more difficult to identify this type of error.

6. Copy and paste the following program into the editor. As you know, the DATA step keyword is misspelled. Also, the semicolon is missing from the PROC PRINT statement, and the PROC MEANS step includes an option that is not valid. As you can see, SAS color-codes the program to indicate the errors.

daat work.newsalesemps;
length First_Name $ 12
Last_Name $ 18 Job_Title $ 25;
infile "&path/newemps.csv" dlm=',';
input First_Name $ Last_Name $
Job_Title $ Salary;
run;
proc print data=work.newsalesemps
run;
proc means data=work.newsalesemps average min;
var Salary;
run;
7. Submit the program and check the log. You should always check the log to make sure that the program ran successfully, even if output is generated.

Notice that there is a WARNING message and the word DAAT is underlined. In this case, SAS resolved the issue by assuming that DAAT was simply DATA misspelled. A warning means that SAS was able to perform the action. In this case, SAS processed the DATA step. But this is a rare situation, as SAS might not always be able to interpret your misspelled words.

Next, notice that the RUN statement is underlined. In this case, the previous line is missing the semicolon. The message 'Syntax error, expecting one of the following...' indicates that something was missing

You might be thinking, “Why did SAS report an error in the RUN statement? There's nothing wrong with the RUN statement.” When you encounter this type of error, always check the statement before the underlined statement. In many cases you will find that the statement before the error is missing a semicolon.

Now look at the next error message. SAS did not recognize the word AVERAGE as a valid option in the PROC MEANS statement, so the PROC MEANS step didn't execute. Notice that SAS lists the valid options. The word MEAN is listed as a valid option and should be used to calculate an average. 

8. In the editor, correct the program.

First, correct the spelling of DATA, and then add a semicolon to the end of the PROC PRINT statement. Lastly, change the word AVERAGE to MEAN in the PROC MEANS statement. data work.newsalesemps;
length First_Name $ 12
Last_Name $ 18 Job_Title $ 25;
infile "&path/newemps.csv" dlm=',';
input First_Name $ Last_Name $
Job_Title $ Salary;
run;
proc print data=work.newsalesemps;
run;
proc means data=work.newsalesemps mean max;
class Job_Title;
var Salary;
run;
9. Submit the revised code and check the log. The log shows that the code ran successfully. No errors or warnings appear. Also, SAS produced the reports you requested. As demonstrated, you can easily view and correct syntax errors in SAS.
PRACTICAL TASK (marked)
Write SAS code that will do the following:
1. Assign a library to a path where you have stored the data.
2. Create a permanent new data set called ‘MaleEmp’.
a. This dataset contains only Male employees from the SALES data.
3. Make a brief SAS report (i.e. Calculate) with the average salary for males
4. Ensure that each section of your SAS code is properly commented.
5. Make sure that your SAS program has no errors and submit it via Stream.