Creating a Raw Data File

Overview

Here are a SAS program and SAS data set that appeared 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;
Notice that 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 have 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 use 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.
Syntax, FILE statement:
FILE file-specification <options> <operating-environment-options>;
  • file-specification can take the form fileref to name a previously defined file reference or 'filename' to point to the actual name and location of the file
  • options names options that are used in creating the output file
  • operating-environment-options names options that are specific to an operating environment.
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;
run;
Instead of identifying the raw data file with a SAS fileref, you can specify the entire filename and location in the FILE statement. For example, the following FILE statement points directly to the Z:sasuser ests.dat file. Note that the path specifying the filename and location must be enclosed in quotation marks.
data _null_; 
   set sasuser.stress; 
   file 'Z:sasuser	ests.dat';
run;

Describing the Data

Whereas the FILE statement specifies the output raw data file, the PUT statement describes the lines to write to the raw data file.
Syntax, PUT statement using column output:
PUT variable startcol-endcol...;
  • variable is the name of the variable whose value is written.
  • startcol indicates where in the line to begin writing the value.
  • endcol indicates where in the line to end the value.
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 text data, the dollar sign ($) is optional.
data _null_; 
   set sasuser.stress; 
   file 'Z:sasuser	ests.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;
Figure 6.13 SAS Data Set sasuser.stress Output with PUT Statement
SAS Data Set sasuser.stress
The resulting raw data file would look like this.
Figure 6.14 Creating a Raw Data File
Creating a Raw Data File
Tip
If you do not execute a FILE statement before a PUT statement in the current iteration of the DATA step, SAS writes the lines to the SAS log.
Tip
SAS assigns the fileref Print to the location where procedure output is being written. If you specify file print in your DATA step, the PUT statement writes the lines to the procedure output location.
Last updated: January 10, 2018
..................Content has been hidden....................

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