Types Of Error In SAS : Syntax Error, Execution Time Error, Data Error, Semantic Error

Many of us are familiar with Errors in SAS and most of the time we have seen the classification of errors in SAS log.
As per the log classification in SAS, There are four type of errors in SAS :
1. SYNTAX ERROR  
2. EXECUTION TIMEERROR   
3.DATA ERROR     
4. SEMANTIC ERROR

1. SYNTAX ERROR : We can have such types of errors whenever our programming statements or codes does not follow the SAS language coding rules. SAS detects SYNTAX Error at COMPILE TIME / STAGE. Syntax errors could be due to the following reasons :
  • Any keyword which is not spelled correctly (Misspelled keyword)
  • Missing or Invalid punctuation
  • Invalid statements or data set options
eg:Quotations marks not used properly (Unmatched)

2. EXECUTION TIME ERROR : Whenever SAS executes a program which processes data, during the processing of data value if any error occurs that is known as EXECUTION TIME ERROR. Most of the execution time errors just gives warning messages or notes in SAS log but These errors allow the program to continue the execution of data. We can identify execution time error usually by location which would be given as line and column numbers in a SAS note or SAS error message. Execution time error could be due to following reasons:



  • Any mathematical operation which is not legal division by zero )
  • Any argument to function which is not legal
  • For BY group processing, when observations are not in proper order
  • While referring to a member of an array which does not exist
  • Open and close error (for brackets and inverted commas) on SAS data sets and other files like INFILE
  • Any INPUT statements which do not match with the cards / datalines 
  • eg: Here we are trying division 80000/0 division by zero )
3. DATA ERROR : We can have such type of errors in SAS, whenever any data values are not as per the SAS statement which we have specified in our SAS program. Suppose we have defined a variable as a numeric but we are passing some data values which are character then SAS gives an error that would be known as DATA ERROR. Data errors occurs during the program execution but these errors allow the program to continue the execution of data and does the following things:
  • It prints the obs under the rule line
  • In the SAS log, It writes an invalid data notes with line number
  • It sets the automatic variable _ERROR_ to for current observation
  • In SAS log, it prints the input line and column number which contain invalid data
  • egHere we entered the text value in numaric field

4. SEMANTIC ERRORS : We can have such type of error in SAS, whenever our programming statements or codes are up to the mark means any SAS statement is correct but any element is not valid for that specific usage. SAS detects SEMANTIC ERRORS at COMPILE TIME / STAGE. Whenever SAS finds any semantic error in program SAS goes for syntax check mode. Semantic errors could be due to the following reasons :
  • Defining or using a numeric variable where only a character variable is applicable
  • For Array, when we use any illegal reference for it
  • Whenever we do not specify correct number of arguments  for any SAS Functions
Ex:  Suppose we are using a libname for a dataset without assigning it prior, for semantic error, libname statement is valid but as it is not specified previously so it would through an error.

DATASTEP OPTIONS IN SAS

DATASTEP OPTIONS ARE:

1.Drop
2. Keep
3.Rename
4.Firstobs
5.Obs
6. Index
7.Label
8.Pw(password)
9.Where

/*1.Drop data step option*/

Excludes variables from processing or from output SAS data sets.
Valid: DATA step and PROC steps.
In DATA steps, the DROP= data set option can apply to both input and output data sets. 
The DROP statement applies only to output data sets.;
/*Ex*/
data est(drop= rept); /*input data set*/
input  sid$ motif$ rept;
datalines;
A00010  AC  5
A00010 TA   6
A00010 GC  3

run;
Drop statement along with set statement: ;
eg:
Data temp;
Set est (drop =sid);/*output data set*/
Run;

/*Drop can be used in proc steps.*/
eg:
Proc print data=EST (drop=motif);
Run;

/*2.Keep= Data Set Option*/

Specifies variables for processing or for writing to output SAS data sets
Valid: DATA step and PROC steps.;
In DATA steps, the KEEP= data set option can apply to both input and output data sets. The KEEP statement applies only to output data sets.In PROC steps, you can use only the KEEP= data set option, not the KEEP statement.
placed in parentheses following data set name and applies 
to that data set only. 

*eg*
data  est1(keep=sid motif ); /*input data set*/
Input sid$ motif$ rept;
Datalines;
A00010   AC   5
A00010  TA    6
;
Run;

/*Keep= option with SET Statement:*/
eg:
Data est2;
   Set est1(keep=sid); /*output data set*/
   Bonus=sid*1.1;
Run;

/*keep can be used in proc steps.*/
eg:
Proc print data=EST (keep=motif);
Run;

/*3.RENAME= Data Set Option:*/

Changes the name of a variable.
The RENAME= data set option can be used in PROC steps and the RENAME statement cannot.
To rename variables before processing begins, 
you must use a RENAME= data set option on the input data set or data sets.
Valid: DATA step and PROC steps.
Syntax:
RENAME= (old-name-1=new-name-1);
Old-name is The variable you want to rename. 
New-name The new name of the variable. 
It must be a valid SAS name.;
/*Ex:*/
Data est2 (rename= (sid=seq_id ));
Input Sid$ motif$ rept;
Datalines;
A00010   AC   5
A00010 TA    6
;
Run;

/*Rename= option with SET Statement:*/
eg:
Data est3;
   Set est2(rename=(motif=mottif_n)); /*output data set*/
   Run;

/*Rename can be used in proc steps.*/
eg:
Proc print data=EST2 (rename=(rept=rept_n));
Run;

/*4.FIRSTOBS= Data Set Option:*/

Specifies which observation SAS processes first.
Valid: DATA step and PROC steps.

/*Syn:*/
FIRSTOBS= MIN | MAX | n|.
/*note*/
in input data set firstobs=data option not valid it works on already existing data set.
The FIRSTOBS= data set option affects a single, 
existing SAS data set. Use the FIRSTOBS= system option to 
affect all steps for the duration of your current SAS session.
You can apply FIRSTOBS= processing to WHERE processing.;

/*firstobs= option with SET Statement:*/
/*Ex*/
Data Temp;
  Set EST (firstobs=3);
Run;

/*We can apply First obs in proc step.*/
eg:
Proc print data=EST (firstobs=2);
Run;

/* firstobs= option with input data set not valid*/
Data est2; (firstobs=3);
Input Sid$ motif$ rept;
Datalines;
A00010   AC   5
A00010 TA    6
A00010   AC   7
A00010 TA    8
;
Run;

/*5.OBS= Data Set Option*/

Specifies which observation SAS processes last.
Syntax
OBS= MIN | MAX | n;
This option specifies the number of the last observation to process, not how many observations should be processed. 
/*It is valid only when an existing SAS data set is read.*/
The OBS= data set option overrides the OBS= system option for the individual data set.While the OBS= data set option specifies an ending point for processing, the FIRSTOBS= data set option specifies a starting point.
You can select observations to be read from external
data files by using the OBS= option in the INFILE statement.;

/*obs= option with SET Statement:*/
/*Ex*/
Data new;
Set a (obs=10);
Run;

/*How to apply in Proc step both firstobs= obs=options   .*/
eg:
Proc print data=a (firstobs=4 obs=15);
/*starting at 4th obs end with 15th obs a has total 19 observations*/
Run;

/*6.INDEX=DATASET OPTION*/
    
Defines indexes when a SAS data set is created.;
syn:
Data dsn (INDEX=(variblename/index-specification-1 ...<index-specification-n>) );
dsn=data set name
/*Ex*/
Data est1 (index=(sid/unique ));
input  sid$ motif$ rept;
datalines;
A00010   AC   5
A00011   TA   6
;
run;

/*7.LABEL= Data Set Option:*/
     Specifies a label for the SAS data set.
The LABEL= data set option enables you to specify labels only for data sets. You can specify labels for the variables in a data set using the LABEL statement. 
The LABEL= option in the ATTRIB statement also enables you to assign labels to variables. 
Label is a text string of up to 256 characters.
/*Syn*/
Data dsn (LABEL='label');
dsn=data set name
/*Ex*/
this label can be view on view table only 
Data  est (label='Expressed Sequence Tags');
Input sid$ motif$ rept;
datalines;
A00010   AC   5
A00010  TA    6
;
run;

proc print;
run;

/*8.PW= Data Set Option:  */
Assigns a read, write, or alter password to a SAS file and 
enables access to a password-protected SAS file.
Valid: DATA step and PROC steps.
The PW= option applies to all types of SAS files except catalogs. 
You can use this option to assign a password to a SAS file or to access a password-protected SAS file.;
/*Ex*/
Data  est (PASSWORD=TIGER);
Input sid$ motif$ rept;
datalines;
A00010   AC 5
A00010  AC  6
A00011  TC  7
;
run;

/*9.WHERE= Data Set Option*/

Use the WHERE= data set option with an input data set to 
select observations that meet the condition specified 
in the WHERE expression before SAS brings them into 
the DATA or PROC step for processing. Selecting observations
that meet the conditions of the WHERE expression is the first 
operation SAS performs in each iteration of the DATA step. 
You can also select observations that are written to an output data set.
The WHERE statement applies to all input data sets, 
whereas the WHERE= data set option selects observations only from 
the data set for which it is specified. 

/*Ex*/
Data  est1 (WHERE=(MOTIF='AC'));
Input sid$ motif$ rept;
datalines;
A00010   AC 5
A00010  AC  6
A00011  TC  7
A00011  TC  8
;
run;

/*Where= option with SET Statement:*/
eg:
Data estn;
   Set est3(where=(rept=6)); /*output data set*/
   Run;

/*Where= can be used in proc steps.*/
eg:
Proc print data=a (where=(age=14));
Run;

Windows In The SAS Windowing Environment Software

What is SAS?

SAS is a statistical software package that allows the user to manipulate and analyze data in many different ways. Because of its capabilities, this software package is used in many disciplines (not just statistics!), including medical sciences,biological sciences, and social sciences.

The SAS Windows:

After you open SAS,There are Five basic SAS windows: the Results and Explorer windows, and three programming windows: Editor, Log, and Output.

Sometimes the windows are not immediately visible (for example, in the Windows operating environment, the Output window comes up behind the Editor and Log windows), but all these windows do exist in your SAS session.


The following figure shows the default view for a Microsoft Windows SAS session, with

pointers to the five main SAS windows


Windows In The SAS Windowing Environment
What is the priority order of SAS windows?

This is one of the frequently asking questions in the Interviews and SAS ,certification, exam.Now we can know what is the priority of SAS windows and Why they have such priority order.



1.The Explorer Window : 


The Explorer window has the first priority in SAS windows ,why because in the Explorer window only store all our data and  gives you easy access to your SAS files and libraries.


2. The Editor Window:

The Editor window has the second  priority in SAS windows ,This window is a text editor. You can use it to type in, edit, and submit SAS programs as well as edit other text files such as raw data files. In Windows operating environments, the default editor is the Enhanced Editor. The Enhanced Editor is syntax sensitive and color codes your programs making it easier to read them and find mistakes.For other operating environments, the default editor is the Program Editor whose features vary with the version of SAS and operating environment.

3. The Log Window:

The Log  window has the Third  priority in SAS windows,in the Log window Every time you run a SAS job, SAS writes messages in your log. Many SAS programmers ignore the SAS log and go straight to the output. That’s understandable, but dangerous contains notes about your SAS session, and after you submit a SAS program, any notes, errors, or warnings associated with your program as well as the program statements themselves will appear in the Log window. 
For good programing each and every time  must check Log window after submits the SAS program.

4.The Output Window:

The Output window has the Fourth  priority in SAS windows,After submitting your program in the SAS windowing environment, your results will go to the Output window If your program generates any printable results, then they will appear in the Output window.

5.The Results window:


The Result window has the Fifth priority in SAS windows,When you have a lot of output, the Results window can be very helpful. The Results window is like a table of contents of your output. It lists each procedure that produces output, and if you open, or expand, the procedure in the Results tree, you can see each part of the procedure output.







Quiz

1. Which of the following lists the steps in the programming process correctly?
a.
1. Write a SAS program.
2. Debug or modify.
3. Run the program.
4. Review the results.
5. Define the business need.
b.
1. Define the business need.
2. Write a SAS program.
3. Run the program.
4. Review the results.
5. Debug or modify.

Correct answer: b

First, you establish the business need for your program. Then you go through the process
of writing and submitting the program, checking your results, and making any necessary
changes. You might have to perform this iterative process more than once.



2. Are raw data files created only by SAS?

a. Yes
b. No

Correct answer: b

Raw data files are files that contain nonsoftwarespecific
data.

3. .Using SAS, you can read any kind of data
a. True
b. False

Correct answer: a

Using SAS, you can read any kind of data. You might have data stored in SAS, in a raw
data file, in Oracle, in Excel, or in other types of files.


4. What does a SAS program file contain?
a. SAS programming code
b. data specific to SAS
c. an embedded Excel worksheet

Correct answer: a

SAS program files contain SAS programming code. These instructions tell SAS how to
process your data and what output to create.

5. Are SAS data sets created only by SAS?
a. Yes
b. No

Correct answer: a
SAS data sets contain data that is created only by SAS and can be read only by SAS.

6. Can a SAS program be saved and reused?
a. Yes
b. No

Correct answer: a
You can save and reuse SAS program files.

7.Which SAS is the centerpiece of all SAS Software?

a.advance SAS
b.Base SAS
c.clinical 
d.financial

correct answer:b
Base SAS is the centerpiece of all SAS software.

8. What is the work flow of  SAS ?
a.
1.manage data
2.analyze data
3.access data
4.present data
b.
1.access data
2.manage data
3.analyze data
4.present data

correct answer:b

1.Access data: Using SAS, you can read any kind of data.

2.Manage data: SAS gives you excellent data management capabilities

.3Analyze data: For statistical analysis. SAS is the gold standard.

4.Present data: You can use SAS to present your data meaningfully.


Review: Exploring the SAS Programming