Chapter 5. Creating SAS Data Sets from Raw Files and Excel Work-sheets

Overview

Introduction

In order to create reports with SAS procedures, your data must be in the form of a SAS data set. If your data is not stored in the form of a SAS data set, you need to create a SAS data set by entering data, by reading raw data, or by accessing files that were created by other software.

This chapter shows you how to design and write DATA step programs to create SAS data sets from raw data that is stored in an external file and from data stored in Microsoft Excel work-sheets. It also shows you how to read data from a SAS data set and write observations out to these sources.

Regardless of the input data source — raw files or Excel work–sheets — you use the DATA step to read in the data and create the SAS data set.

Introduction

Objectives

In this chapter, you learn to

  • reference a SAS library

  • reference a raw data file

  • name a SAS data set to be created

  • specify a raw data file to be read

  • read standard character and numeric values in fixed fields

  • create new variables and assign values

  • select observations based on conditions

  • read instream data

  • submit and verify a DATA step program

  • read a SAS data set and write the observations out to a raw data file.

  • use the DATA step to create a SAS data set from an Excel work-sheet

  • use the SAS/ACCESS LIBNAME statement to read from an Excel work-sheet

  • create an Excel work-sheet from a SAS data set

  • use the CONTENTS procedure to create an Excel work-sheet from a SAS data set

  • use the IMPORT procedure to read external files

Raw Data Files

A raw data file is an external text file whose records contain data values that are organized in fields. Raw data files are non-proprietary and can be read by a variety of software programs. The sample raw data files in this chapter are shown with a ruler to help you identify where individual fields begin and end. The ruler is not part of the raw data file.

Raw Data Files

The table below describes the record layout for a raw data file that contains readings from exercise stress tests that have been performed on patients at a health clinic. Exercise physiologists in the clinic use the test results to prescribe various exercise therapies. The file contains fixed fields. That is, values for each variable are in the same location in all records.

Field Name

Starting Column

Ending Column

Description of Field

ID

1

4

patient ID number

Name

6

25

patient name

RestHR

27

29

resting heart rate

MaxHR

31

33

maximum heart rate during test

RecHR

35

37

recovery heart rate after test

TimeMin

39

40

time, complete minutes

TimeSec

42

43

time, seconds

Tolerance

45

45

comparison of stress test tolerance between this test and the last test (I=increased, D=decreased, S=same, N=no previous test)

Steps to Create a SAS Data Set From a Raw Data File

Let's take a look at the steps for creating a SAS data set from a raw data file. In the first part of this chapter, you will follow these steps to create a SAS data set from a raw data file that contains fixed fields.

Before reading raw data from a file, you might need to reference the SAS library in which you will store the data set. Then you can write a DATA step program to read the raw data file and create a SAS data set.

To read the raw data file, the DATA step must provide the following instructions to SAS:

  • the location or name of the external text file

  • a name for the new SAS data set

  • a reference that identifies the external file

  • a description of the data values to be read.

After using the DATA step to read the raw data, you can use a PROC PRINT step to produce a list report that displays the data values that are in the new data set.

The table below outlines the basic statements that are used in a program that reads raw data in fixed fields. Throughout this chapter, you'll see similar tables that show sample SAS statements.

To do this...

Use this SAS statement...

Reference SAS data library

LIBNAME statement

Reference external file

FILENAME statement

Name SAS data set

DATA statement

Identify external file

INFILE statement

Describe data

INPUT statement

Execute DATA step

RUN statement

List the data

PROC PRINT statement

Execute final program step

RUN statement

You can also use additional SAS statements to perform tasks that customize your data for your needs. For example, you may want to create new variables from the values of existing variables.

Referencing a SAS Library

Using a LIBNAME Statement

As you begin to write the program, remember that you might need to use a LIBNAME statement to reference the permanent SAS library in which the data set will be stored.

To do this...

Use this SAS statement...

Example

Reference a SAS library

LIBNAME statement

libname taxes

'c:acctqtr1 eport';

For example, the LIBNAME statement below assigns the libref Taxes to the SAS library C:AcctQtr1Report in the Windows environment.

libname taxes 'c:acctqtr1
eport';

You do not need to use a LIBNAME statement in all situations. For example, if you are storing the data set in a temporary SAS data set or if SAS has automatically assigned the libref for the permanent library that you are using.

Using a LIBNAME Statement

Referencing a Raw Data File

Using a FILENAME Statement

Before you can read your raw data, you must point to the location of the external file that contains the data. You use the FILENAME statement to point to this location.

To do this...

Use this SAS statement...

Example

Reference a SAS

library LIBNAME statement

libname libref

'SAS-data-library';

Reference an external file

FILENAME statement

filename tests

'c:users mill.dat';

Just as you assign a libref by using a LIBNAME statement, you assign a fileref by using a FILENAME statement.

Filerefs perform the same function as librefs: they temporarily point to a storage location for data. However, librefs reference SAS data libraries, whereas filerefs reference external files.

Using a FILENAME Statement

Defining a Fully Qualified Filename

The following FILENAME statement temporarily associates the fileref Tests with the external file that contains the data from the exercise stress tests. The complete filename is specified as C:UsersTmill.dat in the Windows environment.

filename tests 'c:users	mill.dat';
Defining a Fully Qualified Filename

Defining an Aggregate Storage Location

You can also use a FILENAME statement to associate a fileref with an aggregate storage location, such as a directory that contains multiple external files.

Defining an Aggregate Storage Location

This FILENAME statement temporarily associates the fileref Finance with the aggregate storage directory C:UsersPersonalFinances:

filename finance 'c:userspersonalfinances';

Viewing Active Filerefs

Like librefs, the filerefs currently defined for your SAS session are listed in the SAS Explorer window.

To view details about a referenced file, double-click File Shortcuts (or select File Shortcuts and then Open from the pop-up menu). Then select View

Viewing Active Filerefs
Details. Information for each file (name, size, type, and host path name) is listed.

Viewing Active Filerefs

Referencing a Fully Qualified Filename

When you associate a fileref with an individual external file, you specify the fileref in subsequent SAS statements and commands.

Referencing a Fully Qualified Filename

Referencing a File in an Aggregate Storage Location

To reference an external file with a fileref that points to an aggregate storage location, you specify the fileref followed by the individual filename in parentheses:

Referencing a File in an Aggregate Storage Location
Referencing a File in an Aggregate Storage Location
infile tax('refund'),

For details on referencing external files stored in aggregate storage locations, see the SAS documentation for your operating environment.

Writing a DATA Step Program

Naming the Data Set

The DATA statement indicates the beginning of the DATA step and names the SAS data set to be created.

To do this...

Use this SAS statement...

Example

Reference a SAS library

LIBNAME statement

libname libref

'SAS-data-library';

Reference an external file

FILENAME statement

filename tests

'c:users mill.dat';

Name a SAS data set

DATA statement

data clinic.stress;

Remember that the SAS data set name is a two-level name. For example, the two-level name Clinic.Admit specifies that the data set Admit is stored in the permanent SAS library to which the libref Clinic has been assigned.

Naming the Data Set

Specifying the Raw Data File

When reading raw data, use the INFILE statement to indicate which file the data is in.

To do this...

Use this SAS statement...

Example

Reference a SAS library

LIBNAME statement

libname libref

'SAS-data-library';

Reference an external file

FILENAME statement

filename tests

'c:users mill.dat';

Name a SAS data set

DATA statement

data clinic.stress;

Identify an external file

INFILE statement

infile tests obs=10;

To read the raw data file to which the fileref Tests has been assigned, you write the following INFILE statement:

infile tests;
Specifying the Raw Data File
infile 'c:irspersonal
efund.dat';

Column Input

In this chapter, you'll be working with column input, the most common input style. Column input specifies actual column locations for values. However, column input is appropriate only in certain situations. When you use column input, your data must be

  • standard character or numeric values

  • in fixed fields.

Standard and Nonstandard Numeric Data

Standard numeric data values can contain only

  • numbers

  • decimal points

  • numbers in scientific or E-notation (2.3E4, for example)

  • plus or minus signs.

Nonstandard numeric data includes

  • values that contain special characters, such as percent signs (%), dollar signs ($), and commas (,)

  • date and time values

  • data in fraction, integer binary, real binary, and hexadecimal forms.

The external file that is referenced by the fileref Staff contains the personnel information for a technical writing department of a small computer manufacturer. The fields contain values for each employee's last name, first name, job title, and annual salary.

Notice that the values for Salary contain commas. So, the values for Salary are considered to be nonstandard numeric values. You cannot use column input to read these values.

Standard and Nonstandard Numeric Data

Fixed-Field Data

Raw data can be organized in several different ways.

This external file contains data that is free-format, meaning data that is not arranged in columns. Notice that the values for a particular field do not begin and end in the same columns. You cannot use column input to read this file.

Fixed-Field Data

This external file contains data that is arranged in columns or fixed fields. You can specify a beginning and ending column for each field. Let's look at how column input can be used to read this data.

Fixed-Field Data
Fixed-Field Data

Describing the Data

The INPUT statement describes the fields of raw data to be read and placed into the SAS data set.

To do this...

Use this SAS statement...

Example

Reference a SAS library

LIBNAME statement

libname libref

'SAS-data-library';

Reference an external file

FILENAME statement

filename tests

'c:users mill.dat';

Name a SAS data set

DATA statement

data clinic.stress;

Identify an external file

INFILE statement

infile tests obs=10;

Describe data

INPUT statement

input ID 1-4 Name $ 6-25

...;

Execute the DATA step

RUN statement

run;

Look at the small data file shown below. For each field of raw data that you want to read into your SAS data set, you must specify the following information in the INPUT statement:

  • a valid SAS variable name

  • a type (character or numeric)

  • a range (starting column and ending column).

Describing the Data

The INPUT statement below assigns the character variable ID to the data in columns 1-4, the numeric variable Age to the data in columns 6-7, the character variable ActLevel to the data in columns 9-12, and the character variable Sex to the data in column 14.

Describing the Data

When you use column input, you can

  • read any or all fields from the raw data file

  • read the fields in any order

  • specify only the starting column for values that occupy only one column.

input ActLevel $ 9-12 Sex $ 14 Age 6-7;
Describing the Data

Specifying Variable Names

Each variable has a name that conforms to SAS naming conventions. Variable names

  • must be 1 to 32 characters in length

  • must begin with a letter (A-Z) or an underscore (_)

  • can continue with any combination of numbers, letters, or underscores.

Let's look at an INPUT statement that uses column input to read the three data fields in the raw data file below.

Specifying Variable Names

The values for the variable that you are naming Age are located in columns 1-2. Because Age is a numeric variable, you do not specify a dollar sign ($) after the variable name.

input Age 1-2

The values for the variable ActLevel are located in columns 3-6. You specify a $ to indicate that ActLevel is a character variable.

input Age 1-2  ActLevel $ 3-6

The values for the character variable Sex are located in column 7. Notice that you specify only a single column.

input Age 1-2 ActLevel $ 3-6  Sex $ 7;

Submitting the DATA Step Program

Verifying the Data

To verify your data, it is a good idea to use the OBS= option in the INFILE statement. Adding OBS=n to the INFILE statement enables you to process only records 1 through n, so you can verify that the correct fields are being read before reading the entire data file.

The program below reads the first ten records in the raw data file referenced by the fileref Tests. The data is stored in a permanent SAS data set, named Sasuser.Stress. Don't forget a RUN statement, which tells SAS to execute the previous SAS statements.

data sasuser.stress;
   infile tests  obs=10;
   input ID 1-4 Name $ 6-25
         RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40
         TimeSec 42-43 Tolerance $ 45;
run;
Verifying the Data

Checking DATA Step Processing

If you submitted the DATA step below in the previous guided practice, it ran successfully.

data sasuser.stress;
   infile tests obs=10;
   input ID 1-4 Name $ 6-25
         RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40
         TimeSec 42-43 Tolerance $ 45;
run;

Messages in the log verified that the raw data file was read correctly. The notes in the log indicate that

  • 10 records were read from the raw data file

  • the SAS data set Sasuser.Stress was created with 10 observations and 8 variables.

Checking DATA Step Processing

Listing the Data Set

The messages in the log seem to indicate that the DATA step program correctly accessed the raw data file. But it is a good idea to look at the ten observations in the new data set before reading the entire raw data file. You can submit a PROC PRINT step to view the data.

To do this...

Use this SAS statement...

Example

Reference a SAS library

LIBNAME statement

libname libref

'SAS-data-library';

Reference an external file

FILENAME statement

filename tests

'c:users mill.dat';

Name a SAS data set

DATA statement

data clinic.stress;

Identify an external file

INFILE statement

infile tests obs=10;

Describe data

INPUT statement

input ID 1--4 Name $

6--25 ...;

Execute the DATA step

RUN statement

run;

List the data

PROC PRINT statement

proc print

data=clinic.stress;

Execute the final program step

RUN statement

run;

The following PROC PRINT step lists the Sasuser.Stress data set.

proc print data=sasuser.stress;
run;

The PROC PRINT output indicates that the variables in the Sasuser.Stress data set were read correctly for the first ten records.

Listing the Data Set

Reading the Entire Raw Data File

Now that you've checked the log and verified your data, you can modify the DATA step to read the entire raw data file. To do so, remove the OBS= option from the INFILE statement and re-submit the program.

data sasuser.stress;
   infile tests;
   input ID 1-4 Name $ 6-25
         RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40
         TimeSec 42-43 Tolerance $ 45;
run;

Invalid Data

When you submit the revised DATA step and check the log, you see a note indicating that invalid data appears for the variable RecHR in line 14 of the raw data file, columns 35-37.

This note is followed by a column ruler and the actual data line that contains the invalid value for RecHR.

Invalid Data

The value Q13 is a data-entry error. It was entered incorrectly for the variable RecHR.

RecHR is a numeric variable, but Q13 is not a valid number. So RecHR is assigned a missing value, as indicated in the log. Because RecHR is numeric, the missing value is represented with a period.

Notice, though, that the DATA step does not fail as a result of the invalid data but continues to execute. Unlike syntax errors, invalid data errors do not cause SAS to stop processing a program.

Assuming that you have a way to edit the file and can justify a correction, you can correct the invalid value and rerun the DATA step. If you did this, the log would then show that the data set Sasuser.Stress was created with 21 observations, 8 variables, and no messages about invalid data.

Invalid Data

After correcting the raw data file, you can list the data again to verify that it is correct.

proc print data=sasuser.stress;
run;
Invalid Data

Whenever you use the DATA step to read raw data, remember the steps that you followed in this chapter, which help ensure that you don't waste resources when accessing data:

  • write the DATA step using the OBS= option in the INFILE statement

  • submit the DATA step

  • check the log for messages

  • view the resulting data set

  • remove the OBS= option and resubmit the DATA step

  • check the log again

  • view the resulting data set again.

Creating and Modifying Variables

So far in this chapter, you've read existing data. But sometimes existing data doesn't provide the information you need. To modify existing values or to create new variables, you can use an assignment statement in any DATA step.

For example, here is an assignment statement that assigns the character value Toby Witherspoon to the variable Name:

Name='Toby Witherspoon';

SAS Expressions

You use SAS expressions in assignment statements and many other SAS programming statements to

  • transform variables

  • create new variables

  • conditionally process variables

  • calculate new values

  • assign new values.

An expression is a sequence of operands and operators that form a set of instructions. The instructions are performed to produce a new value:

  • Operands are variable names or constants. They can be numeric, character, or both.

  • Operators are special-character operators, grouping parentheses, or functions. You can learn about functions in Chapter 14, "Transforming Data with SAS Functions," on page 413.

Using Operators in SAS Expressions

To perform a calculation, you use arithmetic operators. The table below lists arithmetic operators.

Operator

Action

Example

Priority

-

negative prefix

negative=-x;

I

**

exponentiation

raise=x**y;

I

*

multiplication

mult=x*y;

II

/

division

divide=x/y;

II

+

addition

sum=x+y;

III

-

subtraction

diff=x-y;

III

When you use more than one arithmetic operator in an expression,

  • operations of priority I are performed before operations of priority II, and so on

  • consecutive operations that have the same priority are performed

    • from right to left within priority I

    • from left to right within priority II and III

  • you can use parentheses to control the order of operations.

Using Operators in SAS Expressions

You use the following comparison operators to express a condition.

Operator

Meaning

Example

= or eq

equal to

name='Jones, C.'

^= or ne

not equal to

temp ne 212

> or gt

greater than

income>20000

< or lt

less than

partno lt "BG05"

>= or ge

greater than or equal to

id>='1543'

<= or le

less than or equal to

pulse le 85

To link a sequence of expressions into compound expressions, you use logical operators, including the following:

Operator

Meaning

AND or &

and, both. If both expressions are true, then the compound expression is true.

OR or |

or, either. If either expression is true, then the compound expression is true.

More Examples of Assignment Statements

The assignment statement in the DATA step below creates a new variable, TotalTime, by multiplying the values of TimeMin by 60 and then adding the values of TimeSec.

data sasuser.stress;
   infile tests;
   input ID 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43
         Tolerance $ 45;
    TotalTime=(timemin*60)+timesec;
run;
More Examples of Assignment Statements

The expression can also contain the variable name that is on the left side of the equal sign, as the following assignment statement shows. This statement re-defines the values of the variable RestHR as 10 percent higher.

data sasuser.stress;
   infile tests;
   input ID 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43
         Tolerance $ 45;
   resthr=resthr+(resthr*.10);
run;

When a variable name appears on both sides of the equal sign, the original value on the right side is used to evaluate the expression. The result is assigned to the variable on the left side of the equal sign.

data sasuser.stress;
   infile tests;
   input ID 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
           RecHR 35-37 TimeMin 39-40 TimeSec 42-43
           Tolerance $ 45;
   resthr=resthr+(resthr*.10);
run;   ^         ^
  result       original value

Date Constants

You can assign date values to variables in assignment statements by using date constants. To represent a constant in SAS date form, specify the date as 'ddmmmyy' or 'ddmmmyyyy' , followed by a D.

Example

In the following program, the second assignment statement assigns a date value to the variable TestDate.

data sasuser.stress;
   infile tests;
   input ID 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43
         Tolerance $ 45;
   TotalTime=(timemin*60)+timesec;
   TestDate='01jan2000'd;
run;
Example
Time='9:25't;
DateTime='18jan2005:9:27:05'dt;

Subsetting Data

As you read your data, you can subset it by processing only those observations that meet a specified condition. To do this, you can use a subsetting IF statement in any DATA step.

The subsetting IF statement causes the DATA step to continue processing only those raw data records or observations that meet the condition of the expression specified in the IF statement. The resulting SAS data set or data sets contain a subset of the original external file or SAS data set.

Example

The subsetting IF statement below selects only observations whose values for Tolerance are D. It is positioned in the DATA step so that other statements do not need to process unwanted observations.

data sasuser.stress;
   infile tests;
   input ID 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43
         Tolerance $ 45;
   if tolerance='D';
   TotalTime=(timemin*60)+timesec;
run;

Because Tolerance is a character variable, the value D must be enclosed in quotation marks, and it must be the same case as in the data set.

Example

Reading Instream Data

Throughout this chapter, our program has contained an INFILE statement that identifies an external file to read.

data sasuser.stress;
   infile tests;
   input ID 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43
         Tolerance $ 45;
   if tolerance='D';
   TotalTime=(timemin*60)+timesec;
  run;

However, you can also read instream data lines that you enter directly in your SAS program, rather than data that is stored in an external file. Reading instream data is extremely helpful if you want to create data and test your programming statements on a few observations that you can specify according to your needs.

To read instream data, you use

  • a DATALINES statement as the last statement in the DATA step (except for the RUN statement) and immediately preceding the data lines

  • a null statement (a single semicolon) to indicate the end of the input data.

data sasuser.stress;
   input ID 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43
         Tolerance $ 45;
   datalines;
   .
   .
   .
data lines go here
   .
   .
   .
;

Example

To read the data for the treadmill stress tests as instream data, you can submit the following program:

data sasuser.stress;
   input ID 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43
         Tolerance $ 45;
   if tolerance='D';
   TotalTime=(timemin*60)+timesec;
   datalines; 
2458 Murray, W            72  185 128 12 38 D 
2462 Almers, C            68  171 133 10  5 I 
2501 Bonaventure, T       78  177 139 11 13 I 
2523 Johnson, R           69  162 114  9 42 S 
2539 LaMance, K           75  168 141 11 46 D 
2544 Jones, M             79  187 136 12 26 N 
2552 Reberson, P          69  158 139 15 41 D 
2555 King, E              70  167 122 13 13 I 
2563 Pitts, D             71  159 116 10 22 S 
2568 Eberhardt, S         72  182 122 16 49 N 
2571 Nunnelly, A          65  181 141 15  2 I 
2572 Oberon, M            74  177 138 12 11 D 
2574 Peterson, V          80  164 137 14  9 D 
2575 Quigley, M           74  152 113 11 26 I 
2578 Cameron, L           75  158 108 14 27 I 
2579 Underwood, K         72  165 127 13 19 S 
2584 Takahashi, Y         76  163 135 16  7 D 
2586 Derber, B            68  176 119 17 35 N 
2588 Ivan, H              70  182 126 15 41 N 
2589 Wilcox, E            78  189 138 14 57 I 
2595 Warren, C            77  170 136 12 10 S 
;
Example

Creating a Raw Data File

Look at the SAS program and SAS data set that you created earlier in this chapter.

data sasuser.stress;
   infile tests;
   input ID 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43
         Tolerance $ 45;
   if tolerance='D';
   TotalTime=(timemin*60)+timesec;
run;
Creating a Raw Data File

As you can see, the data set has been modified with SAS statements. If you wanted to write the new observations to a raw data file, you could reverse the process that you've been following and write out the observations from a SAS data set as records or lines to a new raw data file.

Using the _NULL_ Keyword

Because the goal of your SAS program is to create a raw data file and not a SAS data set, it is inefficient to list a data set name in the DATA statement. Instead, use the keyword _NULL_, which enables you to use the DATA step without actually creating a SAS data set. A SET statement specifies the SAS data set that you want to read from.

data _null_;
    set sasuser.stress;

The next step is to specify the output file.

Specifying the Raw Data File

You use the FILE and PUT statements to write the observations from a SAS data set to a raw data file, just as you used the INFILE and INPUT statements to create a SAS data set. These two sets of statements work almost identically.

When writing observations to a raw data file, use the FILE statement to specify the output file.

For example, if you want to read the Sasuser.Stress data set and write it to a raw data file that is referenced by the fileref Newdat, you would begin your program with the following SAS statements.

data _null_;
   set sasuser.stress;
   file newdat;

Instead of identifying the raw data file with a SAS fileref, you can choose to specify the entire filename and location in the FILE statement. For example, the following FILE statement points directly to the C:ClinicPatientsStress.dat file. Note that the path specifying the filename and location must be enclosed in quotation marks.

data _null_;
   set sasuser.stress;
   file 'c:clinicpatientsstress.dat';

Describing the Data

Whereas the FILE statement specifies the output file, the PUT statement describes the lines to write to the raw data file.

In general, the PUT statement mirrors the capabilities of the INPUT statement. In this case you are working with column output. Therefore, you need to specify the variable name, starting column, and ending column for each field that you want to create. Because you are creating raw data, you don't need to follow character variable names with a dollar sign ($).

data _null_;
   set sasuser.stress;
   file 'c:clinicpatientsstress.dat';
   put id 1-4 name   6-25 resthr 27-29 maxhr 31-33 
        rechr 35-37 timemin 39-40 timesec 42-43 
        tolerance 45 totaltime 47-49;
run;
Describing the Data

The resulting raw data file would look like this.

Describing the Data

In later chapters you'll learn how to use INPUT and PUT statements to read and write raw data in other forms and record types.

Describing the Data

Additional Features

In this section, you learned to read raw data by writing an INPUT statement that uses column input. You also learned how to write to a raw data file by using the FILE statement with column input. However, column input is appropriate only in certain situations. When you use column input, your data must be

  • standard character and numeric values. If the raw data file contains nonstandard values, then you need to use formatted input, another style of input. To learn about formatted input, see Chapter 12, "Reading SAS Data Sets," on page 345.

  • in fixed fields. That is, values for a particular variable must be in the same location in all records. If your raw data file contains values that are not in fixed fields, you need to use list input. To learn about list input, see Chapter 18, "Reading Free-Format Data," on page 547.

Other forms of the INPUT statement enable you to read

  • nonstandard data values such as hexadecimal, packed decimal, SAS date values, and monetary values that contain dollar signs and commas

  • free-format data (data that is not in fixed fields)

  • implied decimal points

  • variable-length data values

  • variable-length records

  • different record types.

Reading Microsoft Excel Data

Overview

In addition to being able to read raw data files, SAS can also read Microsoft Excel data. Whether the input data source is a SAS data set, a raw data file, or a file from another application, you use the DATA step to create a SAS data set. The difference between reading these various types of input is in how you reference the data. To read in Excel data you use one of the following methods:

  • SAS/ACCESS LIBNAME statement

  • Import Wizard

Remember, the Base SAS LIBNAME statement associates a SAS name (libref) with a SAS DATA library by pointing to its physical location. But, the SAS/ACCESS LIBNAME statement associates a SAS name with an Excel workbook file by pointing to its location.

In doing so, the Excel workbook becomes a new library in SAS, and the work-sheets in the workbook become the individual SAS data sets in that library.

The figure below illustrates the difference between how the two LIBNAME statements treat the data.

Overview

The next figure shows how the DATA step is used with three types of input data.

Overview

Notice how the INFILE and INPUT statements are used in the DATA step for reading raw data, but the SET statement is used in the DATA step for reading in the Excel work-sheets.

Running SAS with Microsoft Excel

  • You must have a licensed copy of SAS/ACCESS Interface to PC Files to use a SAS/ACCESS LIBNAME statement that references an Excel workbook.

  • If you are running SAS version 9.1 or earlier and want to read in Microsoft Excel data, you must be using Microsoft Excel 2003 or earlier.

  • To read Microsoft Excel 2007 data you must be running SAS version 9.2 or later.

  • The examples in this section are based on SAS version 9.2 running with Microsoft Excel 2007.

Steps for Reading Excel Data

Let's take a look at the steps for reading in an Excel workbook file.

To read the Excel workbook file, the DATA step must provide the following instructions to SAS:

  • a libref to reference the Excel workbook to be read

  • the name and location (using the libref) of the new SAS data set

  • the name of the Excel work-sheet that is to be read

The table below outlines the basic statements that are used in a program that reads Excel data and creates a SAS data set from an Excel work-sheet. The PROC CONTENTS and PROC PRINT statements are not requirements for reading in Excel data and creating a SAS data set. However, these statements are useful for confirming that your Excel data has successfully been read into SAS.

To do this...

Use this SAS statement...

Example

Reference an Excel workbook file

SAS/ACCESS LIBNAME statement

libname results

'c:usersexercise.xlsx';

Output the contents of the SAS Library

PROC CONTENTS

proc contents data=results._all_;

Execute the PROC CONTENTS statement

RUN statement

run;

Name and create a new SAS data set

DATA statement

data work.stress;

Read in an Excel worksheet (as the input data for the new SAS data set)

SET statement

set results.'ActLevel$'n;

Execute the DATA step

RUN statement

run;

View the contents of a particular data set

PROC PRINT

proc print data=results;

Execute the PROC PRINT statement

RUN statement

run;

The SAS/ACCESS LIBNAME Statement

The general form of the SAS/ACCESS LIBNAME statement is as follows:

Referencing an Excel Workbook

This example uses data similar to the scenario used for the raw data in the previous section. The data shows the readings from exercise stress tests that have been performed on patients at a health clinic.

The stress test data is located in an Excel workbook named exercise.xlsx (shown below), which is stored in the location c:users.

Referencing an Excel Workbook

Notice in the sample work-sheet above that the date column is defined in Excel as dates. That is, if you right click on the cells and select Format Cells (in Excel), the cells have a category of Date. SAS reads this data just as it is stored in Excel. If the date had been stored as text in Excel, then SAS would have read it as a character string.

To read in this workbook, you must first create a libref to point to the workbook's location:

libname results 'c:usersexercise.xlsx';

The LIBNAME statement creates the libref results, which points to the Excel workbook exercise.xlsx. The workbook contains two work-sheets, tests and ActLevel, which are now stored in the new SAS library (results) as data sets.

After submitting the LIBNAME statement, you can look in the SAS Explorer window to see how SAS handles your Excel workbook. The Explorer window enables you to manage your files in the SAS windowing environment.

Referencing an Excel Workbook

Name Literals

In the figure above, notice how the LIBNAME statement created a permanent library, results, which is the SAS name (libref) we gave to the workbook file and its location. The new library contains two SAS data sets, which contain the data from the Excel worksheets. From this window you can browse the list of SAS libraries or display the descriptor portion of a SAS data set.

Notice that the Excel work-sheet names have the special character ($) at the end. All Excel worksheets are designated this way. But remember, special characters such as these are not allowed in SAS data set names by default. So, in order for SAS to allow this character to be included in the data set name, it assigns a name literal to the data set name. A SAS name literal is a name token that is expressed as a string within quotation marks, followed by the uppercase or lowercase letter n. The name literal tells SAS to allow the special character ($) in the data set name.

Name Literals

Named Ranges

A named range is a range of cells within a work-sheet that you define in Excel and assign a name to. In the example below, the worksheet contains a named range, tests_week_1, which SAS recognizes as a data set.

The named range, tests_week_1, and its parent work-sheet, tests, will appear in the SAS Explorer window as separate data sets with the same name, except that the data set created from the named range will have no dollar sign ($) appended to its name.

For more information on named ranges, see your Microsoft Excel documentation.

Named Ranges

Using PROC CONTENTS

In addition to using the SAS explorer window to view library data, you can also use the CONTENTS procedure with the _ALL_ keyword to produce information about a data library and its contents. In the example below, PROC CONTENTS outputs summary information for the SAS data set tests, including data set name, variables, data types, and other summary information. This statement is useful for making sure that SAS is successfully reading in your Excel data before moving on to the DATA step.

proc contents data=results._all_;
run;
Using PROC CONTENTS

About the sample output above:

  • The variables in the data set are pulled from the Excel column headings. SAS uses underscores to replace the spaces.

  • The Excel dates in the Format column are converted to SAS dates with the default DATE9 format.

Creating the DATA Step

You use the DATA statement to indicate the beginning of the DATA step and name the SAS data set to be created. Remember that the SAS data set name is a two-level name. For example, the two-level name results.Admit specifies that the data set Admit is stored in the permanent SAS library to which the libref results has been assigned.

When reading Excel data, use the STEP statement to indicate which work-sheet in the data set you want to read. To read in the Excel file you write the DATA and SET statements as follows:

data work.stress;
   set results.'ActLevel$'n;
run;

In this example, the DATA statement tells SAS to name the new data set, stress, and store it in the temporary library WORK. The SET statement in the DATA step specifies the libref (reference to the Excel file) and the work-sheet name as the input data.

You can use several statements in the DATA step to subset your data as needed. Here, the WHERE statement is used with a variable to include only those participants whose activity level is HIGH.

data work.stress;
   set results.'ActLevel$'n;
   where ActLevel='HIGH';
run;

The figure below shows the partial output for this DATA step in table format.

Creating the DATA Step

Using PROC PRINT

After using the DATA step to read in the Excel data and create the SAS data set, you can use PROC PRINT to produce a list report that displays the data set values.

You can also use the PRINT procedure to refer to a specific work-sheet. Remember to use the name literal when referring to a specific Excel work-sheet. In the example below, the first PRINT statement displays the data values for the new data set that was created in the DATA step. The second PRINT statement displays the contents of the Excel work-sheet that was created by the LIBNAME statement.

proc print data=work.stress;
run;
proc print data=results.'ActLevel$'n;
run;

LIBNAME Statement Options

There are several options that you can use with the LIBNAME statement to control how SAS interacts with the Excel data. The general form of the SAS/ACCESS LIBNAME statement (with options) is as follows:

libname libref 'location-of-Excel-workbook' <options>;

Example:

libname doctors 'c:clinicNotesaddresses.xlsx' <mixed=yes>;

DBMAX_TEXT=n

indicates the length of the longest character string where n is any integer between 256 and 32,767 inclusive. Any character string with a length greater than this value is truncated. The default is 1024.

GETNAMES=YES|NO

determines whether SAS will use the first row of data in an Excel work-sheet or range as column names.

YES specifies to use the first row of data in an Excel work-sheet or range as column names.

NO specifies not to use the first row of data in an Excel work-sheet or range as column names. SAS generates and uses the variable names F1, F2, F3, and so on.

The default is YES.

MIXED=YES|NO

Specifies whether to import data with both character and numeric values and convert all data to character.

YES specifies that all data values will be converted to character.

NO specifies that numeric data will be missing when a character type is assigned. Character data will be missing when a numeric data type is assigned.

The default is NO.

SCANTEXT=YES|NO

specifies whether to read the entire data column and use the length of the longest string found as the SAS column width.

YES scans the entire data column and uses the longest string value to determine the SAS column width.

NO does not scan the column and defaults to a width of 255.

The default is YES.

SCANTIME=YES|NO

specifies whether to scan all row values in a date/time column and automatically determine the TIME. format if only time values exist.

YES specifies that a column with only time values be assigned the TIME8. format.

NO specifies that a column with only time values be assigned the DATE9. format.

The default is NO.

USEDATE=YES|NO

specifies whether to use the DATE9. format for date/time values in Excel workbooks.

YES specifies that date/time values be assigned the DATE9. format.

NO specifies that date/time values be assigned the DATETIME. format.

The default is YES.

Creating Excel Work-sheets

In addition to being able to read Excel data, SAS can also create Excel work-sheets from SAS data sets.

To do this, you use the SAS/ACCESS LIBNAME statement. For example, to create a new worksheet named high_stress from the temporary SAS data set work.high_stress and save this work-sheet in the new Excel file newExcel.xlsx, you would submit the following LIBNAME statement and DATA step:

libname clinic 'c:users
ewExcel.xlsx';
data clinic.high_stress;
   set work.high_stress;
   where ActLevel='HIGH';
run;

The IMPORT Wizard

Importing Data

As an alternative to using programming statements, you can use the Import Wizard to guide you through the process of creating a SAS data set from both raw data and from Excel work-sheets. The Import Wizard enables you to create a SAS data set from different types of external files, such as

  • dBase files (*.dbf)

  • Excel 2007 (or earlier version) workbooks (*.xls, *.xlsx, *.xlsb, or *.xlsm)

  • Microsoft Access tables (*.mdb, *.accdb)

  • Delimited files (*.*)

  • Comma-separated values (*.csv).

Importing Data

To access the Import Wizard, select File

Importing Data
Import Data from the menu bar. The Import Wizard opens with the Select import type screen.

Importing Data

Follow the instructions on each screen of the Import Wizard to read in your data. If you need additional information, select the Help button at the bottom of each screen in the wizard.

Just as you can create a SAS data set from raw data by using the Import Wizard, you can use the Export Wizard to read data from a SAS data set and to write the data to an external data source. To access the Export Wizard, select File

Importing Data
Export Data from the menu bar.

As an alternative to using programming statements, you can use the Import Wizard to guide you through the process of creating a SAS data set from raw data. The Import Wizard enables you to create a SAS data set from different types of external files, such as

  • Excel Spreadsheets (*.xls)

  • Text files (*.txt,*.csv, *.tab, *.asc).

To access the Import Wizard, select Tools

Importing Data
Import Data. The window opens to the Data Type page.

Importing Data

Follow the instructions on each screen of the Import Wizard to read in your data. As an alternative to using programming statements, you can use the Import Data task to guide you through the process of creating a SAS data set from raw data. The Import Data task enables you to create a SAS data set from different types of external files, such as

  • Excel Spreadsheets (*.xls)

  • Text files (*.txt,*.csv, *.tab, *.asc).

To access the Import Data window, select Tools

Importing Data
Import Data. The window opens to the Data Type page.

Importing Data

Follow the instructions on each screen of the Import Data window to read in your data. As an alternative to using programming statements, you can use the Import Data task to guide you through the process of creating a SAS data set from external files. The Import Data task enables you to create a SAS data set from different types of external files, such as

  • Excel Spreadsheets (*.xls)

  • Text files (*.txt,*.csv, *.tab, *.asc).

To access the Import Data window, select File

Importing Data
Import Data and select the location of your data. Double-click the file you want to import. The Import Data task opens to the Region to importpage.

Importing Data

Follow the instructions on each screen of the Import Data window to read in your data.

Chapter Summary

Text Summary

Raw Data Files

A raw data file is an external file whose records contain data values that are organized in fields. The raw data files in this chapter contain fixed fields.

Steps to Create a SAS Data Set

You need to follow several steps to create a SAS data set using raw data. You need to

  • reference the raw data file to be read

  • name the SAS data set

  • identify the location of the raw data

  • describe the data values to be read.

Referencing a SAS Library

To begin your program, you might need to use a LIBNAME statement to reference the SAS library in which your data set will be stored.

Writing a DATA Step Program

The DATA statement indicates the beginning of the DATA step and names the SAS data set(s) to be created.

Next, you specify the raw data file by using the INFILE statement. The OBS= option in the INFILE statement enables you to process a specified number of observations.

This chapter teaches column input, the most common input style. Column input specifies actual column locations for data values. The INPUT statement describes the raw data to be read and placed into the SAS data set.

Submitting the Program

When you submit the program, you can use the OBS= option with the INFILE statement to verify that the correct data is being read before reading the entire data file.

After you submit the program, view the log to check the DATA step processing. You can then list the data set by using the PROC PRINT procedure.

Once you've checked the log and verified your data, you can modify the DATA step to read the entire raw data file by removing the OBS= option from the INFILE statement.

If you are working with a raw data file that contains invalid data, the DATA step continues to execute. Unlike syntax errors, invalid data errors do not cause SAS to stop processing a program. If you have a way to edit the invalid data, it's best to correct the problem and rerun the DATA step.

Creating and Modifying Variables

To modify existing values or to create new variables, you can use an assignment statement in any DATA step. Within assignment statements, you can specify any SAS expression.

You can use date constants to assign dates in assignment statements. You can also use SAS time constants and SAS datetime constants in assignment statements.

Subsetting Data

To process only observations that meet a specified condition, use a subsetting IF statement in the DATA step.

Reading Instream Data

To read instream data lines instead of an external file, use a DATALINES statement or a CARDS statement and enter data directly in your SAS program. Omit the RUN at the end of the DATA step.

Creating a Raw Data File

When the goal of your SAS program is to create a raw data file and not a SAS data set, it is inefficient to list a data set name in the DATA statement. Instead use the keyword _NULL_, which allows the power of the DATA step without actually creating a SAS data set. A SET statement specifies the SAS data set that you want to read from.

You can use the FILE and PUT statements to write out the observations from a SAS data set to a raw data file just as you used the INFILE and INPUT statements to create a SAS data set. These two sets of statements work almost identically.

Microsoft Excel Files

You can read Excel work-sheets by using the SAS/ACCESS LIBNAME statement.

Steps to Create a SAS Data Set from Excel data

You need to follow several steps to create a SAS data set using Excel. You need to

  • provide a name for the new SAS data set

  • the location or name of the libref and Excel work-sheet

  • name the SAS data set

Referencing a SAS Library

To begin your program, you need to use a LIBNAME statement to reference the Excel workbook.

Writing a DATA Step Program

The DATA statement indicates the beginning of the DATA step and names the SAS data set(s) to be created.

Next, you specify the Excel work-sheet to be read by using the SET statement. You must use a SAS name literal since SAS uses the special character ($) to name Excel work-sheets.

Submitting the Program

When you submit the program, you can use the CONTENTS procedure to explore the new library and contents.

After you submit the program, view the log to check the DATA step processing. You can then list the data sets created from the Excel work-sheets by using the PROC PRINT procedure.

Once you've checked the log and verified your data, you can modify the DATA step along with the WHERE statement to subset parts of the data as needed.

Syntax

Reading Data from a Raw File or Reading Instream Data

    LIBNAME libref 'SAS-data-library';

    FILENAME fileref 'filename';

    DATA SAS-data-set;

            INFILE file-specification<OBS=n>;

            INPUT variable < $> startcol-endcol...;

            IF expression;

            variable=expression;

    DATALINES;

            instream data goes here if used

    ;

    RUN; /* not used with the DATALINES statement */ PROC PRINT DATA= SAS-data set;

    RUN;

Creating a Raw Data File

    LIBNAME libref 'SAS-data-library';

    DATA_NULL_;

    SET SAS-data-set;

        FILE fileref or 'filename';

        PUT variable startcol-endcol...;

    RUN;

Reading Data from an Excel Workbook

    LIBNAME libref. '<location-of-Excel-workbook>';

    PROC CONTENTS DATA= SAS-data set;

    DATA output-SAS-data-set;

    SET libref.'worksheet_name$'n;

    WHERE where-expression;

    RUN;

    PROC PRINT DATA= SAS-data set;

    RUN;

Sample Programs

Reading Data from an External File

libname clinic 'c:ethesdapatientsadmit';
 filename admit 'c:clinicpatientsadmit.dat';
data clinic.admittan;
   infile admit obs=5;
   input ID 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43
         Tolerance $ 45;
   if tolerance='D';
   TotalTime=(timemin*60)+timesec;
run;
proc print data=clinic.admittan;
run;

Reading Instream Data

libname clinic 'c:ethesdapatientsadmit';
data clinic.group1;
   input ID 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43
         Tolerance $ 45;
   if tolerance='D';
   TotalTime=(timemin*60)+timesec;
datalines;
2458 Murray, W            72  185 128 12 38 D
2462 Almers, C            68  171 133 10  5 I
2501 Bonaventure, T       78  177 139 11 13 I
2523 Johnson, R           69  162 114  9 42 S
2539 LaMance, K           75  168 141 11 46 D
2544 Jones, M             79  187 136 12 26 N
2595 Warren, C            77  170 136 12 10 S
;
proc print data=clinic.group1;
run;

Reading Excel Data

libname clinic 'c:ethesdapatientsadmit.xlsx'   mixed=yes;
proc contents data=clinic._all_;
run;
data clinic.group1;
   set clinic.'worksheet1$'n;
   where tolerance='D';
   label TotalTime='Total Time';
proc print data=clinic.group1;
run;

Creating Excel Work-sheets

libname healthxls 'c:usersyears.xlsx'mixed=yes;
data healthxls.qtr1_2009;
   set health.qtr1_2009;
run;
proc contents data=health._all_;
run;

Points to Remember

  • LIBNAME and FILENAME statements are global. Librefs and filerefs remain in effect until you change them, cancel them, or end your SAS session.

  • For each field of raw data that you read into your SAS data set, you must specify the following in the INPUT statement: a valid SAS variable name, a type (character or numeric), a starting column, and if necessary, an ending column.

  • When you use column input, you can read any or all fields from the raw data file, read the fields in any order, and specify only the starting column for variables whose values occupy only one column.

  • Column input is appropriate only in some situations. When you use column input, your data must be standard character and numeric values, and these values must be in fixed fields. That is, values for a particular variable must be in the same location in all records.

Chapter Quiz

Select the best answer for each question. After completing the quiz, you can check your answers using the answer key in the appendix.

  1. Which SAS statement associates the fileref Crime with the raw data file C:StatesDataCrime?

    1. filename crime 'c:statesdatacrime';

    2. filename crime c:statesdatacrime;

    3. fileref crime 'c:statesdatacrime';

    4. filename 'c:statesdatacrime' crime;

  2. Filerefs remain in effect until . . .

    1. you change them.

    2. you cancel them.

    3. you end your SAS session.

    4. all of the above

  3. Which statement identifies the name of a raw data file to be read with the fileref Products and specifies that the DATA step read only records 1-15?

    1. infile products obs 15;

    2. infile products obs=15;

    3. input products obs=15;

    4. input products 1-15;

  4. Which of the following programs correctly writes the observations from the data set below to a raw data file?

    Chapter Quiz
    1. data _null_;
         set work.patients;
         infile 'c:clinicpatients
      eferals.dat';
         input id 1-4 sex 6 $ age 8-9 height 11-12
             weight 14-16 pulse 18-20;
      run;
    2. data referals.dat;
         set work.patients;
         input id 1-4 sex $ 6 age 8-9 height 11-12
             weight 14-16 pulse 18-20;
      run;
    3. data _null_;
         set work.patients;
         file c:clinicpatients
      eferals.dat;
         put id 1-4 sex 6 $ age 8-9 height 11-12
               weight 14-16 pulse 18-20;
      run;
    4. data _null_;
         set work.patients;
         file 'c:clinicpatients
      eferals.dat';
         put id 1-4 sex 6 $ age 8-9 height 11-12
             weight 14-16 pulse 18-20;
      run;
  5. Which raw data file can be read using column input?

    1. Chapter Quiz
    2. Chapter Quiz
    3. Chapter Quiz
    4. all of the above.

  6. Which program creates the output shown below?

    Chapter Quiz
    1. data work.salesrep;
         infile empdata;
         input ID $ 1-4 LastName $ 6-12
               FirstName $ 14-18 City $ 20-29;
      run;
      proc print data=work.salesrep;
      run;
    2. data work.salesrep;
         infile empdata;
         input ID $ 1-4 Name $ 6-12
               FirstName $ 14-18 City $ 20-29;
      run;
      proc print data=work.salesrep;
      run;
    3. data work.salesrep;
         infile empdata;
         input ID $ 1-4 name1 $ 6-12
               name2 $ 14-18 City $ 20-29;
      run;
      proc print data=work.salesrep;
      run;
    4. all of the above.

  7. Which statement correctly reads the fields in the following order: StockNumber, Price, Item, Finish, Style?

    Field Name

    Start Column

    End Column

    Data Type

    StockNumber

    1

    3

    character

    Finish

    5

    9

    character

    Style

    11

    18

    character

    Item

    20

    24

    character

    Price

    27

    32

    numeric

    Chapter Quiz
    1. input StockNumber $ 1-3 Finish $ 5-9 Style $ 11-18
            Item $ 20-24 Price 27-32;
    2. input StockNumber $ 1-3 Price 27-32
            Item $ 20-24 Finish $ 5-9 Style $ 11-18;
    3. input $ StockNumber 1-3 Price 27-32   $
            Item   20-24 $ Finish 5-9 $ Style 11-18;
    4. input StockNumber $ 1-3 Price $ 27-32
            Item $ 20-24 Finish $ 5-9 Style $ 11-18;
  8. Which statement correctly re-defines the values of the variable Income as 100 percent higher?

    1. income=income*1.00;

    2. income=income+(income*2.00);

    3. income=income*2;

    4. income=*2;

  9. Which program correctly reads instream data?

    1. data finance.newloan;
         input datalines;
         if country='JAPAN';
         MonthAvg=amount/12;
      1998 US     CARS   194324.12
      1998 US     TRUCKS 142290.30
      1998 CANADA CARS    10483.44
      1998 CANADA TRUCKS  93543.64
      1998 MEXICO CARS    22500.57
      1998 MEXICO TRUCKS  10098.88
      1998 JAPAN   CARS   15066.43
      1998 JAPAN   TRUCKS 40700.34
      ;
    2. data finance.newloan;
         input Year 1-4 Country $ 6-11
               Vehicle $ 13-18 Amount 20-28;
         if country='JAPAN';
         MonthAvg=amount/12;
         datalines;
      run;
    3. data finance.newloan;
         input Year 1-4 Country 6-11
               Vehicle 13-18 Amount 20-28;
         if country='JAPAN';
         MonthAvg=amount/12;
         datalines;
      1998 US     CARS   194324.12
      1998 US     TRUCKS 142290.30
      1998 CANADA CARS    10483.44
      1998 CANADA TRUCKS  93543.64
      1998 MEXICO CARS    22500.57
      1998 MEXICO TRUCKS  10098.88
      1998 JAPAN   CARS   15066.43
      1998 JAPAN   TRUCKS 40700.34
      ;
    4. data finance.newloan;
         input Year 1-4 Country $ 6-11
               Vehicle $ 13-18 Amount 20-28;
         if country='JAPAN';
         MonthAvg=amount/12;
         datalines;
      1998 US     CARS   194324.12
      1998 US     TRUCKS 142290.30
      1998 CANADA CARS    10483.44
      1998 CANADA TRUCKS  93543.64
      1998 MEXICO CARS    22500.57
      1998 MEXICO TRUCKS  10098.88
      1998 JAPAN   CARS   15066.43
      1998 JAPAN   TRUCKS 40700.34
      ;
  10. Which SAS statement subsets the raw data shown below so that only the observations in which Sex (in the second field) has a value of F are processed?

    Chapter Quiz
    1. if sex=f;

    2. if sex=F;

    3. if sex='F';

    4. a or b

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset