User Preferences

Content preview.

Arcu felis bibendum ut tristique et egestas quis:

  • Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
  • Duis aute irure dolor in reprehenderit in voluptate
  • Excepteur sint occaecat cupidatat non proident

Keyboard Shortcuts

17.1 - the output statement.

An OUTPUT statement overrides the default process by telling SAS to output the current observation when the OUTPUT statement is processed — not at the end of the DATA step. The OUTPUT statement takes the form:

where you may name as few or as many data sets as you like. If you use an OUTPUT statement without specifying a data set name, SAS writes the current observation to each of the data sets named in the DATA step. Any data set name appearing in the OUTPUT statement must also appear in the DATA statement.

The OUTPUT statement is pretty powerful in that, among other things, it gives us a way:

  • to write observations to multiple data sets
  • to control the output of observations to data sets based on certain conditions
  • to transpose datasets using the OUTPUT statement in conjunction with the RETAIN statement, BY group processing, and the LAST.variable statement.

Throughout the rest of this section, we'll look at examples that illustrate how to use OUTPUT statements correctly. We'll work with the following subset of the ICDB Study's log data set:

The icblog data set

As you can see, this log data set contains four variables:

  • subj : the subject's identification number
  • v_type : the type of clinic visit, which means the number of months since the subject was first seen in the clinic
  • v_date : the date of the clinic visit
  • form : codes that indicate the data forms that were completed during the subject's clinic visit

The log data set is a rather typical data set that arises from large national clinical studies in which there are a number of sites around the country where data are collected. Typically, the clinical sites collect the data on data forms and then "ship" the data forms either electronically or by mail to a centralized location called a Data Coordinating Center (DCC). As you can well imagine, keeping track of the data forms at the DCC is a monumental task. For the ICDB Study, for example, the DCC received more than 68,000 data forms over the course of the study.

In order to keep track of the data forms that arrive at the DCC, they are "logged" into a database and subsequently tracked as they are processed at the DCC. In reality, a log database will contain many more variables than we have in our subset, such as the dates the data on the forms were entered into the database, who entered the data, the dates the entered data was verified, who verified the data, and so on. To keep our lives simple, we'll just use the four variables described above.

Example 17.1 Section  

This example uses the OUTPUT statement to tell SAS to write observations to data sets based on certain conditions. Specifically, the following program uses the OUTPUT statement to create three SAS data sets — s210006 , s310032 , and s410010 — based on whether the subject identification numbers in the icdblog data set meet a certain condition:

As you can see, the DATA statement contains three data set names — s210006 , s310032 , and s410010 . That tells SAS that we want to create three data sets with the given names. The SET statement, of course, tells SAS to read observations from the permanent data set called stat481.icdblog . Then comes the IF-THEN-ELSE and OUTPUT statements that make it all work. The first IF-THEN tells SAS to output any observations pertaining to subject 210006 to the s210006 data set; the second IF-THEN tells SAS to output any observations pertaining to subject 310032 to the s310032 data set; and, the third IF-THEN statement tells SAS to output any observations pertaining to subject 410010 to the s410010 data set. SAS will hiccup if you have a data set name that appears in an OUTPUT statement without it also appearing in the DATA statement.

The PRINT procedures, of course, tell SAS to print the three newly created data sets. Note that the last PRINT procedure does not have a DATA= option. That's because when you name more than one data set in a single DATA statement, the last name on the DATA statement is the most recently created data set, and the one that subsequent procedures use by default. Therefore, the last PRINT procedure will print the s410010 data set by default.

Now, before launching and running the SAS program, right-click to save the icdblog data set to a convenient location on your computer. Then, launch the SAS program and edit the LIBNAME statement so that it reflects the location in which you saved the data set. Then, run    the program and review the output from the PRINT procedures. You should see that, as expected, the data set s210006 contains data on subject 210006; the data set s310032 contains data on subject 310032; and s410010 contains data on subject 410010.

Incidentally, note that the IF-THEN-ELSE construct used here in conjunction with the OUTPUT statement is comparable to attaching the WHERE= option to each of the data sets appearing in the DATA statement.

Example 17.2 Section  

Using an OUTPUT statement suppresses the automatic output of observations at the end of the DATA step. Therefore, if you plan to use any OUTPUT statements in a DATA step, you must use OUTPUT statements to program all of the output for that step. The following SAS program illustrates what happens if you fail to direct all of the observations to output:

The DATA statement contains two data set names, subj210006 and subj310032 , telling SAS that we intend to create two data sets. However, as you can see, the IF statement contains an OUTPUT statement that directs output to the subj210006 data set, but no OUTPUT statement directs output to the subj310032 data set. Launch and run    the SAS program to convince yourself that the subj210006 data set contains data for subject 210006, while the subj310032 data set contains 0 observations. You should see a message like this in the log window:

as well as see that no output for the subj310032 data set appears in the output window.

Example 17.3 Section  

If you use an assignment statement to create a new variable in a DATA step in the presence of OUTPUT statements, you have to make sure that you place the assignment statement before the OUTPUT statements. Otherwise, SAS will have already written the observation to the SAS data set, and the newly created variable will be set to missing. The following SAS program illustrates an example of how two variables, current and days_vis , get set to missing in the output data sets because their values get calculated after SAS has already written the observation to the SAS data set:

The main thing to note in this program is that the current and days_vis assignment statements appear after the IF-THEN-ELSE and OUTPUT statements. That means that each observation will be written to one of the three output data sets before the current and days_vis values are even calculated. Because SAS sets variables created in the DATA step as missing at the beginning of each iteration of the DATA step, the values of current and days_vis will remain missing for each observation.

By the way, the today ( ) function, which is assigned to the variable current , creates a date variable containing today's date. Therefore, the variable days_vis is meant to contain the number of days since the subject's recorded visit v_date . However, as described above, the values of current and days_vis get set to missing. Launch and run    the SAS program to convince yourself that the current and days_vis variables in the subj310032 data set contain only missing values. If we were to print the subj210006 and subj410020 data sets, we would see the same thing.

The following SAS program illustrates the corrected code for the previous DATA step, that is, for creating new variables with assignment statements in the presence of OUTPUT statements:

Now, since the assignment statements precede the OUTPUT statements, the variables are correctly written to the output data sets. That is, now the variable current contains the date in which the program was run and the variable days_vis contains the number of days since that date and the date of the subject's visit. Launch and run    the SAS program to convince yourself that the current and days_vis variables are properly written to the subj310032 data set. If we were to print the subj210006 and subj410020 data sets, we would see similar results.

Example 17.4 Section  

After SAS processes an OUTPUT statement within a DATA step, the observation remains in the program data vector and you can continue programming with it. You can even output the observation again to the same SAS data set or to a different one! The following SAS program illustrates how you can create different data sets with some of the same observations. That is, the data sets created in your DATA statement do not have to be mutually exclusive:

The symptoms data set

The DATA step creates two temporary data sets, symptoms and visitsix . The symptoms data set contains only those observations containing a form code of sympts . The visitsix data set, on the other hand, contains observations for which v_type equals 6. The observations in the two data sets are therefore not necessarily mutually exclusive. In fact, launch and run    the SAS program and review the output from the PRINT procedures. Note that the observation for subject 410010 in which form = sympts is contained in both the symptoms and visitsix data sets.

IMAGES

  1. SAS assignment statements 2

    assignment statement in sas

  2. SAS writing example

    assignment statement in sas

  3. Learn SAS by examples: Learn SAS by example: Chapter 16

    assignment statement in sas

  4. 40 Most Common SAS Statements, Functions and Procedures

    assignment statement in sas

  5. How to Use the INFILE Statement in SAS (With Example)

    assignment statement in sas

  6. Lesson 5 SAS set statement

    assignment statement in sas

VIDEO

  1. SAS OnDemand for Academics: Reading in Raw Data (Datalines & Infile Statements)

  2. Exploring Input Data and Replacing Missing Values

  3. What Is PDV In Base SAS Programming?

  4. SAS Training

  5. SAS Day 2

  6. Lesson 5 SAS set statement

COMMENTS

  1. Statements: Assignment Statement

    Variable can be a variable name, array reference, or SUBSTR function. Variables that are created by the Assignment statement are not automatically retained. is any SAS expression. expression can contain the variable that is used on the left side of the equal sign. When a variable appears on both sides of a statement, the original value on the ...

  2. Lesson 4: Assignment Statements and Numeric Functions

    The standard form of any SAS function is: functionname (argument1, argument2,…); For example, if we want to add three variables, a, b, and c, using the SAS function SUM and assign the resulting value to a variable named d, the correct form of our assignment statement is: d = sum (a, b, c) ;

  3. 5. SAS Variables and Assignment Statements

    5.1. Assignment Statement Basics. The fundamental method of modifying the data in a data set is by way of a basic assignment statement. Such a statement always takes the form: variable = expression; where variable is any valid SAS name and expression is the calculation that is necessary to give the variable its values.

  4. SAS Variables: Ways to Create Variables

    In a DATA step, you can create a new variable and assign it a value by using it for the first time on the left side of an assignment statement. SAS determines the length of a variable from its first occurrence in the DATA step. The new variable gets the same type and length as the expression on the right side of the assignment statement.

  5. 4.1

    The following SAS program illustrates a very simple assignment statement in which SAS adds up the four exam scores of each student and stores the result in a new numeric variable called examtotal. DATA grades; input name $ 1-15 e1 e2 e3 e4 p1 f1; * add up each students four exam scores. and store it in examtotal;

  6. Lesson 4: Assignment Statements and Numeric Functions

    Upon completing this lesson, you should be able to do the following: write a basic assignment statement involving a numeric variable. write an assignment statement that involves an arithmetic calculation. write an assignment statement that utilizes one of the many numeric SAS functions that are available. describe how SAS handles missing values ...

  7. SAS Help Center: Assignment

    SAS® 9.4 DATA Step Statements: Reference documentation.sas.com. ... Assignment statements evaluate the expression on the right side of the equal sign and store the result in the variable that is specified on the left side of the equal sign.

  8. Adding Information to a SAS Data Set

    The basic method of adding information to a SAS data set is to create a new variable in a DATA step with an assignment statement. An assignment statement has the form: variable = expression; ... This statement directs SAS to read the value of AirCost, add 10 to it, and assign the result to the new variable, NewAirCost. ...

  9. SAS Help Center: Assignment Statement

    SAS® Viya™ 3.1 Statements: Reference documentation.sas.com SAS® Help Center. Customer Support ... Assignment statements evaluate the expression on the right side of the equal sign and store the result in the variable that is specified on the left side of the equal sign.

  10. SAS Help Center: How the DATA Step Works: A Basic Introduction

    The DATA statement begins the process of building a SAS data set and names the data set. The statements that make up the DATA step are compiled, and the syntax is checked. If the syntax is correct, then the statements are executed. In its simplest form, the DATA step is a loop with an automatic output and return action.

  11. SAS Variables : Creating Variables

    In a DATA step, you can create a new variable and assign it a value by using it for the first time on the left side of an assignment statement. SAS determines the length of a variable from its first occurrence in the DATA step. The new variable gets the same type and length as the expression on the right side of the assignment statement.

  12. how to add a new variable in an assignment statement

    First make sure to use actual variable names. You cannot assign values to both NEW and VARIABLE in one assignment statement. And you would need an operator or something between the variables THAT and VARIABLE. You don't use ASSIGNMENT statements in SQL. You do that in DATA steps.

  13. SAS Help Center: Simple Assignment Statements

    Simple Assignment Statements. Simple assignment statements involve an equation that has a matrix name on the left side and either an expression or a function that generates a matrix on the right side. Suppose that you want to generate some statistics for the weekly coffee data. If a cup of coffee costs 30 cents, then you can create a matrix ...

  14. SAS Help Center: Assignment Statement

    SAS® Viya™ 3.1: DS2 Language Reference documentation.sas.com. SAS® Help Center. Customer Support SAS Documentation. SAS® Visual Data Mining and Machine Learning 8.1 | 8.1. PDF EPUB Feedback. Differences in the SAS 9 and SAS Viya Platforms. An Introduction to SAS Viya Programming for SAS 9 Programmers ...

  15. SAS Help Center: Assignment Statement

    SAS® DATA Step Statements: Reference documentation.sas.com. SAS® Help Center. Customer Support SAS Documentation. SAS® Viya® Programming Documentation | 2021.2.3. PDF EPUB Feedback. This documentation is for a version of the software that is not covered by Standard Support. ...

  16. Assignment Statement :: SAS(R) 9.3 Statements: Reference

    names a new or existing variable. Range:variable can be a variable name, array reference, or SUBSTR function. Tip: Variables that are created by the Assignment statement are not automatically retained. expression. is any SAS expression. Tip:expression can contain the variable that is used on the left side of the equal sign.

  17. 17.1

    If you use an assignment statement to create a new variable in a DATA step in the presence of OUTPUT statements, you have to make sure that you place the assignment statement before the OUTPUT statements. Otherwise, SAS will have already written the observation to the SAS data set, and the newly created variable will be set to missing.

  18. Step-by-Step Programming with Base SAS (R) Software

    Programming a rounding calculation with only the arithmetic operators is a lengthy process. However, SAS contains around 280 built-in numeric expressions called functions. You can use them in expressions just as you do the arithmetic operators. For example, the following assignment statement rounds the value of AirCost to the nearest $50:

  19. SAS Help Center: Assignment Statement

    Details. Assignment statements evaluate the expression on the right side of the equal sign and store the result in the variable that is specified on the left side of the equal sign. The following type conversions take place with the Assignment statement: If the variable has a data type and the expression's data type does not match and can be ...

  20. Problem with assignment statement

    Re: Problem with assignment statement. Posted 07-22-2015 04:01 PM (1881 views) | In reply to Dahcilsam. No data step. The data step would start with DATA . The Proc Import creates a data set but does no manipulation of data and ends at the RUN; Add code such as TLK suggested.

  21. Using SAS Libraries: Assigning SAS Libraries

    To use a particular SAS library within a SAS program, the library must be identified to SAS. This process, termed assigning a library, involves the following steps: identifying the library to SAS. In most cases, this identification is accomplished by specifying a logical name, or libref, by which such items as SAS statements and procedures can ...