|
|
|
||||
|
|
|||||
This unit will look at more errors in writing SAS programs and the symptoms (error messages in the SAS Log) that the errors cause. Here we will focus on errors you can make in the DATA step.
1.1 About the
dataWe will be using the 'c:\sas\hs1' and hsbstat files we have been using.
A common error is to misspell a variable name, for example when you go to make a new variable. Below we try to create total from read + write + mat except that the last variable should be math.
DATA hsbtotal; set 'c:\sas\hs1'; total = read + write + mat ; RUN;
When we run this, we don't get any errors in the log window, but we get the following notes. The note NOTE: Variable mat is uninitialized. is the way that SAS says "I have never heard of the variable mat". This is almost always a serious error and should be remedied. Also, SAS is telling us that 200 missing values were generated for the variable total (since mat did not exist, it was set to missing).
61 DATA hsbtotal; 62 set 'c:\sas\hs1'; 63 total = read + write + mat ; 64 RUN; NOTE: Variable mat is uninitialized. NOTE: Missing values were generated as a result of performing an operation on missing values. Each place is given by: (Number of times) at (Line):(Column). 200 at 63:24 NOTE: There were 200 observations read from the data set 'c:\sas\hs1'. NOTE: The data set WORK.HSBTOTAL has 200 observations and 13 variables. NOTE: DATA statement used: real time 0.04 seconds cpu time 0.02 secondsIn this case, we will change mat to math and this works fine.
DATA hsbtotal; set 'c:\sas\hs1'; total = read + write + math ; RUN;
A common error is forgetting the SET statement when you go to make a new data file based on an existing SAS data file. For example, below we want to create the variable total based on 'c:\sas\hs1' , but forget the SET statement to read in the 'c:\sas\hs1' data file.
DATA hsbtotal; total = read + write + math ; RUN;
output window, and the log window says.When we run this, we don't get any output in the
54 DATA hsbtotal;
55 total = read + write + math ;
56 RUN;
NOTE: Variable write is uninitialized.
NOTE: Variable read is uninitialized.
NOTE: Variable math is uninitialized.
NOTE: Missing values were generated as a result of performing an operation on missing values.
Each place is given by: (Number of times) at (Line):(Column).
1 at 55:16 1 at 55:24
NOTE: The data set WORK.HSBTOTAL has 1 observations and 4 variables.
NOTE: DATA statement used:
real time 0.07 seconds
cpu time 0.03 seconds
Note the messages NOTE: Variable write is uninitialized, etc. like we saw above. These are saying the variables we mentioned when we went to compute total do not exist, and of course they don't exist because we did not use the SET statement to read in the file that contains those variables. Below we add the SET statement and this runs fine.
DATA hsbtotal; set 'c:\sas\hs1'; total = read + write + math ; RUN;
Consider the example below and see if you can figure out what is wrong.
DATA myhs ;
SET 'c:\sas\hs1' ;
honpub = .;
if (honcomp = 1) and (public=1) then honpub = 1;
else honpub = 0;
RUN;
The variable honcomp does not exist in c:\sas\hs1 but it does exist in the temporary file hs2 that we created.
Consider the example below and see if you can figure out what is wrong.
DATA hsbtotal; set 'c:\sas\hs1'; RUN;
total = read + write + math ;PROC MEANS DATA=hsbtotal; VAR total; RUN;
We created the variable total outside of a data step.
Consider the example below and see if you can figure out what is wrong. This is very tricky, both to identify and to fix.
TITLE "get means of read write and math ; PROC MEANS DATA='c:\sas\hs1'; VAR read write math; RUN;
We forgot the close quote at the end of the title statement and the rest of the commands are considered part of the title. We fix this by entering
";
and then running that to close the quote.
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