The Little sas book 1.3 The Two Parts of a SAS Program SAS programs are constructed from two basic building blocks: DATA steps and PROC steps. a typical program starts with a DATA step to create a SAS data set and then passes the data to a PROC step for processing. Here is a simple progra that converts miles to kilometers in a DATA step and prints the results with a PROC step Proc DATA step PROC step PROC PRINT DATA.distance RUN DATA and PROC steps are made up of statements. A step may have as few as one or as many as hundreds of statements. Most statements work in only one type of step- -in DATA steps but not PROC steps, or vice versa. A common mistake made by beginners is to try to use a statement in the wrong kind of step. You're not likely to make this mistake if you remember that DATA step read and modify data while PROC steps analyze data, perform utility functions, or print reports DATA steps start with the DATA statement, which starts, not surprisingly, with the word step above produces a SAS data set named DISTANCE. In addition to reading data from TA ATA. This keyword is followed by a name that you make up for a SAS data set. The DA external, raw data files, DATA steps can include DO loops, IF-THEN/ELSE logic, and a larg assortment of numeric and character functions. DATA steps can also combine data sets in just about any way you want, including concatenation and match-merge Procedures, on the other hand, start with a PROC statement in which the keyword PROCis followed by the name of the procedure(PRINT, SORT, or MEANS, for example). Most SAS procedures have only a handful of possible statements Like following a recipe, you use basically the same statements or ingredients each time. SAS procedures do everything from simple sorting and printing to analysis of variance and 3D graphics. Other SAS procedures perform utility functions such as importing data files and data entry state ends when SAS encounters a new step( marked by a DATA or PROC statement), a RUN SAS to run all the precedi nning in batch mode, the end of the program. RUN statements tell tement lines of the step and are among those rare, global statements that are not part of a DATA or PROC step. In the program above, SAS knows that the DATA step has ended when it reaches the PROC statement. The PROC step ends with a RUN statement, which coincides with the end of the program. If you use SAS long enough, you may run into an exception. Steps can also terminate with a QUIT, STOP, or ABORT
6 The Little SAS Book PROC A DAT 1.3 The Two Parts of a SAS Program SAS programs are constructed from two basic building blocks: DATA steps and PROC steps. A typical program starts with a DATA step to create a SAS data set and then passes the data to a PROC step for processing. Here is a simple program that converts miles to kilometers in a DATA step and prints the results with a PROC step: DATA and PROC steps are made up of statements. A step may have as few as one or as many as hundreds of statements. Most statements work in only one type of stepin DATA steps but not PROC steps, or vice versa. A common mistake made by beginners is to try to use a statement in the wrong kind of step. You’re not likely to make this mistake if you remember that DATA steps read and modify data while PROC steps analyze data, perform utility functions, or print reports. DATA steps start with the DATA statement, which starts, not surprisingly, with the word DATA. This keyword is followed by a name that you make up for a SAS data set. The DATA step above produces a SAS data set named DISTANCE. In addition to reading data from external, raw data files, DATA steps can include DO loops, IF-THEN/ELSE logic, and a large assortment of numeric and character functions. DATA steps can also combine data sets in just about any way you want, including concatenation and match-merge. Procedures, on the other hand, start with a PROC statement in which the keyword PROC is followed by the name of the procedure (PRINT, SORT, or MEANS, for example). Most SAS procedures have only a handful of possible statements. Like following a recipe, you use basically the same statements or ingredients each time. SAS procedures do everything from simple sorting and printing to analysis of variance and 3D graphics. Other SAS procedures perform utility functions such as importing data files and data entry. A step ends when SAS encounters a new step (marked by a DATA or PROC statement), a RUN statement, or, if you are running in batch mode, the end of the program.1 RUN statements tell SAS to run all the preceding lines of the step and are among those rare, global statements that are not part of a DATA or PROC step. In the program above, SAS knows that the DATA step has ended when it reaches the PROC statement. The PROC step ends with a RUN statement, which coincides with the end of the program. 1 If you use SAS long enough, you may run into an exception. Steps can also terminate with a QUIT, STOP, or ABORT statement
Chapter 1: Getting Started Using SAs Software 7 can stack building blocks in any order, you can arrange DATA and PROC steps in any order.As While a typical program starts with a DAta step to input or modify data and then passes the da a PROC step, that is certainly not the only pattern for mixing DatA and PROC steps. Just as you program could even contain only DATA steps or only PROC steps To review, the table below outlines the basic differences between DATA and PROC steps: DATA steps PROC steps begin with DATA statements begin with PROC statements read and modify data perform specific analysis or function create a sas data set produce results or report As you read this table, keep in mind that it is a simplification. Because SAS is so flexible, the differences between DATA and PROC steps are, in reality, more blurry. The table above is not meant to imply that PROC steps never create SAS data sets(many do), or that DATA steps never produce reports(they can). Nonetheless, you will find it much easier to write SAS programs if you derstand the basic functions of DATA and PROC steps
Chapter 1: Getting Started Using SAS Software 7 While a typical program starts with a DATA step to input or modify data and then passes the data to a PROC step, that is certainly not the only pattern for mixing DATA and PROC steps. Just as you can stack building blocks in any order, you can arrange DATA and PROC steps in any order. A program could even contain only DATA steps or only PROC steps. To review, the table below outlines the basic differences between DATA and PROC steps: DATA steps PROC steps begin with DATA statements begin with PROC statements read and modify data perform specific analysis or function create a SAS data set produce results or report As you read this table, keep in mind that it is a simplification. Because SAS is so flexible, the differences between DATA and PROC steps are, in reality, more blurry. The table above is not meant to imply that PROC steps never create SAS data sets (many do), or that DATA steps never produce reports (they can). Nonetheless, you will find it much easier to write SAS programs if you understand the basic functions of DATA and PROC steps
The Little sas book 1.4 The data Steps built-in loop DATA steps read and modify data, and they do it in a way that is flexible, giving you lots of control over what happens to your data. However, DATA steps also have an underlying structure, an implicit, built-in loop. You dont tell SAS to execute this loop: SAS does it automatically Memorize this DATA steps execute line by line and observation by observation. This basic concept is rarely stated explicitly. Consequently, new users often grow into old users before they figure this out on their own The idea that DATA steps execute line by line is fairly straightforward and easy to understand. It means that, by default, SAS executes line one of your DATA step before it executes line two, and line two before line three, and so on. That seems common sense, and yet new users frequently run into problems because they try to use a variable before they create it. If a variable named Z is the product of X and Y, then you better make sure that the statements creating X and Y come What is not so obvious is that while DATA steps execute line by line, they also execute observation by observation. That means SAs takes the first observation and runs it all the way through the DATA step(line by line, of course)before looping back to pick up the second observation. In this way, SAS sees only one observation at a time. Imagine a SAS program running in slow motion: SAS reads observation number one from you input data set. Then SAS executes your DATA step using that observation. If SAS reaches the end of the DATA step without encountering any serious errors, then SaS writes the current the next observation. After the last observation has been written to the output data set, SAS terminates the DATA step and moves on to the next step, if there is one. End of slow motion; please return to normal megahertz. This diagram illustrates how an observation flows through a DATA step nput data set DATA step output data set observation line 1 observation 1 observation 2 observation 2 observation 3 line 3 observation 3 line 4 line 5
8 The Little SAS Book 1.4 The DATA Step’s Built-in Loop DATA steps read and modify data, and they do it in a way that is flexible, giving you lots of control over what happens to your data. However, DATA steps also have an underlying structure, an implicit, built-in loop. You don’t tell SAS to execute this loop: SAS does it automatically. Memorize this: DATA steps execute line by line and observation by observation. This basic concept is rarely stated explicitly. Consequently, new users often grow into old users before they figure this out on their own. The idea that DATA steps execute line by line is fairly straightforward and easy to understand. It means that, by default, SAS executes line one of your DATA step before it executes line two, and line two before line three, and so on. That seems common sense, and yet new users frequently run into problems because they try to use a variable before they create it. If a variable named Z is the product of X and Y, then you better make sure that the statements creating X and Y come before the statements creating Z. What is not so obvious is that while DATA steps execute line by line, they also execute observation by observation. That means SAS takes the first observation and runs it all the way through the DATA step (line by line, of course) before looping back to pick up the second observation. In this way, SAS sees only one observation at a time. Imagine a SAS program running in slow motion: SAS reads observation number one from your input data set. Then SAS executes your DATA step using that observation. If SAS reaches the end of the DATA step without encountering any serious errors, then SAS writes the current observation to a new, output data set and returns to the beginning of the DATA step to process the next observation. After the last observation has been written to the output data set, SAS terminates the DATA step and moves on to the next step, if there is one. End of slow motion; please return to normal megahertz. This diagram illustrates how an observation flows through a DATA step: DATA step line 1 line 2 line 3 line 4 line 5 output data set observation 1 observation 2 observation 3 input data set observation 1 observation 2 observation 3
Chapter 1: Getting Started Using SAs Software 9 SAS reads observation number one and processes it using line one of the DATA step, then line he output data set. This diagram shows the first execution of the line-by-line loop. Once sAs in two, and so on until SAS reaches the end of the DAtA step. Then SAS writes the observation finishes with the first observation, it loops back to the top of the DATA step and picks up observation two. When SAS reaches the last observation, it automatically stops. Here is an analogy. DATA step processing is a bit like voting. When you arrive at your polling place, you stand in line behind other people who have come to vote. When you reach the front of the line you are asked standard questions: What is your name? Where do you live? "Then you sign your name, and you cast your vote. In this analogy, the people are observations, and the voting process is the DATA step. People vote one at a time(or observation by observation). Each roters choices are secret, and peeking at your neighbors ballot is definitely frowned upon. In addition, each person completes each step of the process in the same order(line by line).You cannot cast your vote before you give your name and address. Everything must be done in the roDer o If this seems a bit too structured, don't worry. You can override the line-by-line and observation-by-observation strue For example, you can use the rEtain statement, discussed in section 3.9, to make data from the observation available to the current observation You can also use the output statement, discussed in sections 6.11 tput da
Chapter 1: Getting Started Using SAS Software 9 SAS reads observation number one and processes it using line one of the DATA step, then line two, and so on until SAS reaches the end of the DATA step. Then SAS writes the observation in the output data set. This diagram shows the first execution of the line-by-line loop. Once SAS finishes with the first observation, it loops back to the top of the DATA step and picks up observation two. When SAS reaches the last observation, it automatically stops.1 Here is an analogy. DATA step processing is a bit like voting. When you arrive at your polling place, you stand in line behind other people who have come to vote. When you reach the front of the line you are asked standard questions: “What is your name? Where do you live?” Then you sign your name, and you cast your vote. In this analogy, the people are observations, and the voting process is the DATA step. People vote one at a time (or observation by observation). Each voter’s choices are secret, and peeking at your neighbor’s ballot is definitely frowned upon. In addition, each person completes each step of the process in the same order (line by line). You cannot cast your vote before you give your name and address. Everything must be done in the proper order. 1 If this seems a bit too structured, don’t worry. You can override the line-by-line and observation-by-observation structure in a number of ways. For example, you can use the RETAIN statement, discussed in section 3.9, to make data from the previous observation available to the current observation. You can also use the OUTPUT statement, discussed in sections 6.11 and 6.12, to control when observations are written to the output data set
The Little sas book 1.5 Choosing a Mode for Submitting SAS Programs So far we have talked about writing SAS programs, but simply writing a program does not give you any results. Just like writing a letter to your representative in Congress does no good unless you mail it, a SAS program does nothing until you submit or execute it. You can execute a SAS program several ways, but not all methods are available for all operating environments. Check in the SAS Help and Documentation for your operating environment or with your SAS Support Consultant to find out which methods are available to you. The method you choose for executing a SaS program will depend on your preferences and on what is most appropriate for you application and your environment. If you are using SAS at a large site with many users, then ask around and find out which is the most accepted method of executing SAS. If you are using SAS on your own personal computer, then choose the method that suits you. SAS windowing environment If you type SAS at your system prompt, or click on the SAs icon, you will most likely get into the SAS windowing environment. In th interactive environment, you can write and edit sas programs, submit programs for processing, and view and print your results. In addition, there are many SAS windows for performing different tasks such as managing SAS files, customizing the interface, accessing SAS Help, and importing or exporting data Exactly what your windowing environment looks like depends on the type of computer or terminal you are using the operating environment on the computer, and what options are in effect when you start up SAS If you are using a personal computer, then the SAS windowing environment will look similar to other programs on your computer, and many of the features will be familiar to you. HR0e-550rE SAS Enterprise Guide If you have SAS EnterpriseGuide oftware, which runs only under windows, you may choose to submit your programs from within SAS Enterprise Guide. To do this, use the Insert menu to open a Code window where you can either enter your SAS program or open an existing SAS program. Then you can choose to run your code on the local machine, or on a remote server where sas is installed. To run your SAS program on a remote server, you must have SAs Integration Technologies software installed. Also, SAS Enterprise Guide can write SAS code for you through its extensive menu system Noninteractive mode Noninteractive mode is where your SAS statements are in a file on your system, and you start up SAS Dut for coffee specifying that you want to execute that file. SAS immediately starts to AS Program process your file and ties up your computer, or window, until it is running finished. The results are usually placed in a file or files, and you are returned to your system prompt. Noninteractive mode is useful in many situations. This mode is good if you want your program to execute immediately, but you do not want nterprise Guide software is also available with SAS Version 8, but is licensed separately
10 The Little SAS Book 1.5 Choosing a Mode for Submitting SAS Programs So far we have talked about writing SAS programs, but simply writing a program does not give you any results. Just like writing a letter to your representative in Congress does no good unless you mail it, a SAS program does nothing until you submit or execute it. You can execute a SAS program several ways, but not all methods are available for all operating environments. Check in the SAS Help and Documentation for your operating environment or with your SAS Support Consultant to find out which methods are available to you. The method you choose for executing a SAS program will depend on your preferences and on what is most appropriate for your application and your environment. If you are using SAS at a large site with many users, then ask around and find out which is the most accepted method of executing SAS. If you are using SAS on your own personal computer, then choose the method that suits you. SAS windowing environment If you type SAS at your system prompt, or click on the SAS icon, you will most likely get into the SAS windowing environment. In this interactive environment, you can write and edit SAS programs, submit programs for processing, and view and print your results. In addition, there are many SAS windows for performing different tasks such as managing SAS files, customizing the interface, accessing SAS Help, and importing or exporting data. Exactly what your windowing environment looks like depends on the type of computer or terminal you are using, the operating environment on the computer, and what options are in effect when you start up SAS. If you are using a personal computer, then the SAS windowing environment will look similar to other programs on your computer, and many of the features will be familiar to you. SAS Enterprise Guide If you have SAS Enterprise Guide software,1 which runs only under Windows, you may choose to submit your programs from within SAS Enterprise Guide. To do this, use the Insert menu to open a Code window where you can either enter your SAS program or open an existing SAS program. Then you can choose to run your code on the local machine, or on a remote server where SAS is installed. To run your SAS program on a remote server, you must have SAS Integration Technologies software installed. Also, SAS Enterprise Guide can write SAS code for you through its extensive menu system. Noninteractive mode Noninteractive mode is where your SAS program statements are in a file on your system, and you start up SAS specifying that you want to execute that file. SAS immediately starts to process your file and ties up your computer, or window, until it is finished. The results are usually placed in a file or files, and you are returned to your system prompt. Noninteractive mode is useful in many situations. This mode is good if you want your program to execute immediately, but you do not want 1 Beginning with SAS 9, SAS Enterprise Guide software is included with Base SAS software, but is installed separately. SAS Enterprise Guide software is also available with SAS Version 8, but is licensed separately