In this example, the DATA step reads an external file
named STUDENT, which contains student data, and 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 SAS view, because SAS 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-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 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 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, appear in the SAS 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 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–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 SAS 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 column 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 number 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 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 library.
|
8 |
The
first PROC PRINT calls the SAS view MYV9LIB.CLASS. The SAS 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.
|