UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

SAS Class Notes 2.0
Entering Data


0.0 Getting the Data

You can download the data files for the SAS class as a Winzip file by clicking on SASdata.zip.  The examples will assume that you have saved the files in c:\sas however you can save the files anywhere you like.  In the examples just replace c:\sas with the folder where you chose to save the files.

1.0 SAS statements and procs in this unit

infile Identifies an external raw data file to read
input Lists variable names in the input file
datalines Indicates internal data
set Reads a SAS data set
proc contents Contents of a data set
proc print Prints observations of variables in a data set

We will start with inputting an Excel file into SAS first through the SAS Import Wizard. The variable names are on the first line of the Excel file.

This method also works for other file formats, such as comma-separated or tab-delimited files. Now we can look at the data or even modify them if we want.

One of more commonly used ASCII data formats is comma-separated-values (.csv) format. Files of this type can be read in through the Import Wizard as shown above or through a little bit of programming. We will now show how to read in a .csv file with a SAS data step. The following segment is the beginning part of the hs0 file in .csv format. This data file doesn't have variable names on the first line of data file.  Also notice that the line in bold italics has two consecutive commas near the end. This means that the value is missing in between. In order to read in the data correctly, we use option dsd in the infile statement.
0,70,4,1,1,"general",57,52,41,47,57
1,121,4,2,1,"vocati",68,59,53,63,61
0,86,4,3,1,"general",44,33,54,58,31
0,141,4,3,1,"vocati",63,44,47,53,56
0,172,4,2,1,"academic",47,52,57,53,61
0,113,4,2,1,"academic",44,52,51,63,61
0,50,3,2,1,"general",50,59,42,53,61
0,11,1,2,1,"academic",34,46,45,39,36
0,84,4,2,1,"general",63,57,54,,51
0,48,3,2,1,"academic",57,55,52,50,51
0,75,4,2,1,"vocati",60,46,51,53,61
0,60,5,2,1,"academic",57,65,51,63,61
0,95,4,3,1,"academic",73,60,71,61,71
The following data step will read the data file and name it temp. We will also print out the first ten observations to check that the inputting was successful.
data temp;
  infile 'c:\sas\hs0.csv' delimiter=',' dsd;
  input gender id race ses schtyp prgtype $ read write math science socst ;
run;
proc print data = temp (obs=10);
run;
The other type of commonly used ASCII data format is fixed format. It always requires a codebook to specify which column corresponds to which variable. Here is a small example of this type of data with a codebook. Notice how we make use of the codebook in the input statement below.
        195  094951
        26386161941
        38780081841
        479700  870
        56878163690
        66487182960
        786  069  0
        88194193921
        98979090781
       107868180801
variable name column number
id 1-2
a1 3-4
t1 5-6
gender 7
a2 8-9
t2 10-11
tgender 12
data fixed;
  infile "c:\sas\schdat.fix";
  input id 1-2 a1 3-4 t1 5-6 gender 7 a2 8-9 t2 10-11 tgender 12;
run;
proc print data = fixed;
run;
Last but not least, sometimes we may want to input data directly from within SAS and here is what to do.
data hsb10;
  input id female race ses schtype $ prog
        read write math science socst;
datalines;
 147 1 1 3 pub 1 47  62  53  53  61
 108 0 1 2 pub 2 34  33  41  36  36
  18 0 3 2 pub 3 50  33  49  44  36
 153 0 1 2 pub 3 39  31  40  39  51
  50 0 2 2 pub 2 50  59  42  53  61
  51 1 2 1 pub 2 42  36  42  31  39
 102 0 1 1 pub 1 52  41  51  53  56
  57 1 1 2 pub 1 71  65  72  66  56
 160 1 1 2 pub 1 55  65  55  50  61
 136 0 1 2 pub 1 65  59  70  63  51
;
run;

proc print data=hsb10;
run;

So far, all the SAS data sets that we have created are temporary. That is when we quit SAS, these data sets will be gone. To save a SAS data file to disk we can do the following.

data 'c:\sas\schdat';
  set fixed;
run;

We can directly read permanent SAS data files.

proc print data='c:\sas\schdat';
run;

Let's use the SAS/Explorer again to see the data sets we have created in this section. We will use hs0 in our next section.

2.0 For More Information


How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California