DATA Step Views


Definition of a DATA Step View

A DATA step view is a native view that has the broadest scope of any SAS data view. It contains stored DATA step programs that can read data from a variety of sources, including:

  • raw data files

  • SAS data files

  • PROC SQL views

  • SAS/ACCESS views

  • DB2, ORACLE, or other DBMS data.

Creating DATA Step Views

In order to create a DATA step view, specify the VIEW= option after the final data set name in the DATA statement. The VIEW= option tells SAS to compile, but not to execute, the source program and to store the compiled code in the input DATA step view that is named in the option.

For example, the following statements create a DATA step view named DEPT.A:

 libname dept  'SAS-data-library';  data dept.a / view=dept.a;  ... more SAS statements    run; 

Note that if the SAS data view exists in a SAS data library, and if you use the same member name to create a new view definition, then the old data view is overwritten.

Beginning with Version 8, DATA step views retain source statements. You can retrieve these statements using the DESCRIBE statement. The following example uses the DESCRIBE statement in a DATA step view in order to write a copy of the source code to the SAS log:

 data viewname view=inventory;     describe;  run; 

For more information on how to create data views and use the DESCRIBE statement, see the DATA statement in SAS Language Reference: Dictionary .

What Can You Do with a DATA Step View?

Using a DATA step view, you can do the following:

  • directly process any file that can be read with an INPUT statement

  • read other SAS data sets

  • generate data without using any external data sources and without creating an intermediate SAS data file.

Because DATA step views are generated by the DATA step, they can manipulate and manage input data from a variety of sources including data from external files and data from existing SAS data sets. The scope of what you can do with a DATA step view, therefore, is much broader than that of other types of SAS data views.

Differences between DATA Step Views and Stored Compiled DATA Step Programs

DATA step views and stored compiled DATA step programs differ in the following ways:

  • a DATA step view is implicitly executed when it is referenced as an input data set by another DATA or PROC step. Its main purpose is to provide data, one record at a time, to the invoking procedure or DATA step.

  • a stored compiled DATA step program is explicitly executed when it is specified by the PGM= option on a DATA statement. Its purpose is usually a more specific task, such as creating SAS data files, or originating a report.

For more information on stored compiled DATA step programs, see Chapter 30, 'Stored Compiled DATA Step Programs,' on page 547.

Restrictions and Requirements

Global statements do not to apply to a DATA step view. Global statements such as the FILENAME, FOOTNOTE, LIBNAME, OPTIONS, and TITLE statements, even if included in the DATA step that created the data view, have no effect on the data view. If you do include global statements in your source program statements, SAS stores the DATA step view but not the global statements. When the view is referenced, actual execution may differ from the intended execution.

For information on using DATA step views created in an earlier release, see Chapter 35, 'SAS 9.1 Compatibility with SAS Files From Earlier Releases,' on page 583.

Performance Considerations

  • DATA step code executes each time that you use a view. This may add considerable system overhead. In addition, you run the risk of having your data change between steps.

  • Depending on how many reads or passes on the data are required, processing overhead increases .

  • When one pass is requested , no data set is created. Compared to traditional methods of processing, making one pass improves performance by decreasing the number of input/output operations and elapsed time.

  • When multiple passes are requested, the view must build a spill file that contains all generated observations so that subsequent passes can read the same data that was read by previous passes.

Example 1: Merging Data to Produce Reports

If you want to merge data from multiple files but you do not need to create a file that contains the combined data, you can create a DATA step view of the combination for use in subsequent applications.

For example, the following statements define DATA step view MYV9LIB.QTR1, which merges the sales figures in the data file V9LR.CLOTHES with the sales figures in the data file V9LR.EQUIP. The data files are merged by date, and the value of the variable Total is computed for each date.

 libname myv9lib 'SAS-data-library';  libname v9lr 'SAS-data-library';  data myv9lib.qtr1 / view=myv9lib.qtr1;     merge v9lr.clothes v9lr.equip;        by date;     total = cl_v9lr + eq_v9lr;  run; 

The following PRINT procedure executes the view:

 proc print data=myv9lib.qtr1;  run; 

Example 2: Producing Additional Output Files

In this example, the DATA step reads an external file named STUDENT, which contains student data, then writes observations that contain known problems to data set MYV9LIB.PROBLEMS. The DATA step also defines the DATA step view MYV9LIB.CLASS. The DATA step does not create a SAS data file named MYV9LIB.CLASS.

The FILENAME and the LIBNAME statements are both global statements and must exist outside of the code that defines the view, because views cannot contain global statements.

Here are the contents of the external file STUDENT:

 dutterono   MAT     3  lyndenall   MAT  frisbee     MAT    94              SCI    95  zymeco      ART    96  dimette            94  mesipho     SCI    55  merlbeest   ART    97  scafernia          91  gilhoolie   ART   303  misqualle   ART    44  xylotone    SCI    96 

Here is the DATA step that produces the output files:

 libname myv9lib 'SAS-data-library';  filename student 'external-file-specification'; [1]  data myv9lib.class(keep=name major credits)     myv9lib.problems(keep=code date) / view=myv9lib.class; [2]  infile student;     input name $ 1-10 major $ 12-14 credits 16-18; [3]  select;  when (name=' ' or major=' ' or credits=.)           do code=01;              date=datetime();              output myv9lib.problems;           end; [4]  when (0<credits<90)           do code=02;              date=datetime();              output myv9lib.problems;           end; [5]  otherwise         output myv9lib.class;     end;  run; [6] 

The following example shows how to print the files created previously. The data view MYV9LIB.CLASS contains the observations from STUDENT that were processed without errors. The data file MYV9LIB.PROBLEMS contains the observations that contain errors.

If the data frequently changes in the source data file STUDENT, there would be different effects on the returned values in the SAS data view and the SAS data file:

  • New records, if error free, that are added to the source data file STUDENT between the time you run the DATA step in the previous example and the time you execute PROC PRINT in the following example, will appear in the data view MYV9LIB.CLASS.

  • On the other hand, if any new records, failing the error tests, were added to STUDENT, the new records would not show up in the SAS data file MYV9LIB.PROBLEM, until you run the DATA step again.

A SAS data view dynamically updates from its source files each time it is used. A SAS data file, each time it is used, remains the same, unless new data is written directly to the file.

 filename student '  external-file-specification'  ;  libname myv9lib '  SAS-data--library'  ; [7]  proc print data=myv9lib.class;  run; [8]  proc print data=myv9lib.problems;     format date datetime18.;  run; [9] 
[1]  

Reference a library called MYV9LIB. Tell SAS where a file that associated with the fileref STUDENT is stored.

[2]  

Create a data file called PROBLEMS and a data view called CLASS and specify the column names for both data sets.

[3]  

Select the file that is referenced by the fileref STUDENT and select the data in character format that resides in the specified positions in the file. Assign column names.

[4]  

When data in the columns NAME, MAJOR or CREDITS is blank or missing, assign a code of 01 to the observation where the missing value occurred. Also assign a SAS datetime code to the error and place the information in a file called PROBLEMS.

[5]  

When the amount of credits is greater than zero, but less than ninety, list the observations as code 02 in the file called PROBLEMS and assign a SAS datetime code to the observation.

[6]  

Place all other observations, which have none of the specified errors, in the SAS data view called MYV9LIB.CLASS.

[7]  

The FILENAME statement assigns the fileref STUDENT to an external file. The LIBNAME statement assigns the libref MYV9LIB to a SAS data library.

[8]  

The first PROC PRINT calls the data view MYV9LIB.CLASS. The data view extracts data on the fly from the file referenced as STUDENT.

[9]  

This PROC PRINT prints the contents of the data file MYV9LIB.PROBLEMS.




SAS 9.1 Language Reference. Concepts
SAS 9.1 Language Reference Concepts
ISBN: 1590471989
EAN: 2147483647
Year: 2004
Pages: 255

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net