SAS Programs
Hello, dear friend, you can consult us at any timeif you have any questions, add WeChat: daixieit
Practicals: An Introduction to SAS Programming
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:
Understanding the SAS Programming Process
Here is the overall process of programming in SAS.
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.
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.
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
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.
SAS detects the end of a step when it encounters one of the following:
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.
SAS statements usually begin with an identifying keyword, and they always end with a semicolon.
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.
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.
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.
a. This dataset contains only Male employees from the SALES data.
2026-03-19