Sas Interview Questions
Sas Interview Questions
Compares just the BY variable present in the dataset. Compares all the variables
present in the dataset.
NODUPKEY NODUP
Syntax: Syntax:
PROC SORT DATA=readin NODUPKEY; PROC SORT DATA=readin NO
BY variable name; BY variable name;
RUN; RUN;
Informat Format
These are used to read the data or take input data from external These are used
files. to write the data.
4)Explain DATA_NULL_?
Answer: As the name defines, DATA_NULL_ is a data step that actually does
not create any data set.
It is used for:
Creating macro variables.
Writing the output without any data set.
6) Name and describe few SAS character functions that are used for data
cleaning in brief.
Answer: Few SAS character functions that are used for data cleaning are
enlisted below:
Compress(char_string) function is used for removing blanks or some
specified characters from a given string.
TRIM(str) function is used for removing trailing blanks from a given
string.
LOWCASE(char_string) function is used for converting all the
characters in a given string to lowercase.
UPCASE(char_string) function is used for converting all the characters
in a given string to uppercase.
COMPBL(str) function is used for converting multiple blanks to a
single blank.
7) Name and describe few SAS character functions that are used for data
cleaning in brief.
Answer: Few SAS character functions that are used for data cleaning are
enlisted below:
Compress(char_string) function is used for removing blanks or some
specified characters from a given string.
TRIM(str) function is used for removing trailing blanks from a given
string.
LOWCASE(char_string) function is used for converting all the
characters in a given string to lowercase.
UPCASE(char_string) function is used for converting all the characters
in a given string to uppercase.
COMPBL(str) function is used for converting multiple blanks to a
single blank.
Answer: As the meaning of the word ‘RETAIN’ signifies to keep the value
once assigned, the purpose of RETAIN statement is the same in SAS
programming as it’s meaning implies.
Within a SAS program, when it is required to move from the current
iteration to the next of the data step, at that time RETAIN statement tells
SAS to retain the values rather than set them to missing.
Ans. Missover – When the Missover option is used on the INFILE statement,
the INPUT statement does not jump to the next line when reading a short
line. When an INPUT statement reaches the end of the current input data
record, variables without any values assigned are set to missing.
Truncover –The Truncover option assigns the raw data value to the variable
even if the value is shorter than the length that is expected by the INPUT
statement. The Truncover option acts similar to the Missover option.
However, it takes partial values to fill the first unfilled variable.
The difference between the two is that while Truncover reads partial data
that falls at the end of the record, Missover sets the value to missing.
ODS outputtype
PATH path name
FILE = Filename and Path
STYLE = StyleName
;
PROC some proc
;
ODS outputtype CLOSE;
5
6
7
8
9
10
Single Trailing @
Double Trailing @ @
.It instructs SAS to hold a record in the input buffer across multiple
iterations of the DATA step.
How to minimize the number of decimal places for the variable using
PROC MEANS. You can limit the decimal places by using MAXDEC=option.
With this, you can set it equal to the length that you prefer.
Q9. What is the length assigned to the target variable by the Scan function?
Ans. The SCAN function returns a given word from a character string using
default and specific delimiters. The length assigned to the target variable by
the scan function is 200.
12
Ans. Following are the five methods to perform “table lookup” in SAS:
Match Merging
Format Tables
Direct Access
PROC SQL
Arrays
Thus, a one-to-one merge is used if both data sets in the merge statement
are sorted and each observation in one data set has a corresponding
observation in the other data set.
drop, keep, rename, label, format, informat, attrib, where, by, retain, length,
array.
Ans=The CEIL function, when issued, retrieves the smallest integer, while
FLOOR does the opposite and retrieves the biggest one.
answer
Answer
ODS Trace is used to find the names of the particular output objects when
several of them are created by some procedure. ODS TRACE ON; ODS
TRACE Off;
ODS Trace is used to find the names of the particular output objects when
several of them are created by some procedure. ODS TRACE ON; ODS
TRACE Off;
Ans=We will use PROC MEANS for numeric variables whereas we use PROC
FREQ for categorical variables.
answer
You can use the OUTPUT statement to save summary statistics in a SAS
data set. This information can then be used to create customized reports or
to save historical information about a process.
What is Debugging?
answer
Debugging is a technique for testing the program logic, and this can be
done with the help of Debugger.
There are some system options that can be used to debug SAS Macros:
MPRINT, MLOGIC, SYMBOLGEN.
When a user wants to specify a command line for the operating system to
execute, the RUN is used. It is also used to make the SAS log readable by
creating a step boundary.
17
Alter= Data set option enables users to access the read-and write-protected
file. It prevents the user from replacing or deleting the file. This is among one
of the most frequently asked SAS interview questions.
There are some system options that can be used to debug SAS Macros:
MPRINT, MLOGIC, SYMBOLGEN.
To display the contents of the SAS dataset PROC print is used and also to
assure that the data were read into SAS correctly. While PROC CONTENTS
display information about a SAS dataset.
SAS INFORMATS are used to read, or input data from external files known
as Flat Files ASCII files, text files or sequential files). The informat will tell
SAS on how to read data into SAS variables.
18
SUBSTRN
This function extracts a substring using the start and end positions. In case
the end
position is not mentioned, it extracts all the characters till the end of the
string.
Syntax
SUBSTRN('stringval',p1,p2)
Following is the description of the parameters used:
stringval is the value of the string variable.
p1 is the start position of extraction.
p2 is the final position of extraction.
Arrays in SAS are used to store and retrieve a series of values using an
index value. The
index represents the location in a reserved memory area.
Syntax
In SAS an array is declared by using the following syntax:
ARRAY ARRAY-NAME(SUBSCRIPT) ($) VARIABLE-LIST ARRAY-VALUES
In the above syntax:
ARRAY is the SAS keyword to declare an array.
ARRAY-NAME is the name of the array which follows the same rule as
variable
names.
SUBSCRIPT is the number of values the array is going to store.
($) is an optional parameter to be used only if the array is going to store
character
values.
VARIABLE-LIST is the optional list of variables which are the place holders
for
array values.
ARRAY-VALUES are the actual values that are stored in the array. They
can be
declared here or can be read from a file or data line.
20
SAS Statements
Let us now discuss the SAS statements:
Statements can start anywhere and end anywhere. A semicolon at the
end of the
last line marks the end of the statement.
Many SAS statements can be on the same line, with each statement
ending with a
semicolon.
Space can be used to separate the components in a SAS program
statement.
SAS keywords are not case sensitive.
Every SAS program must end with a RUN statement.
1.
DO Index
The loop continues from the start value till the stop value of the index
variable.
21
2.
DO WHILE
The loop continues till the while condition becomes false.
3.
DO UNTIL
The loop continues till the UNTIL condition becomes True.
Syntax
DO UNTIl (variable condition);
. . . SAS statements . . . ;
END;
1.
IF Statement
An if statement consists of a condition. If the condition is true then the
specific
data is fetched.
2.
IF-THEN-ELSE Statement
An if statement followed by else statement, which executes when the
Boolean
condition is false.
3.
IF-THEN-ELSE-IF Statement
An if statement followed by else statement, which is again followed by
another
pair of IF-THEN Statement.
4.
IF-THEN-DELETE Statement
An if statement consists of a condition, which when true deletes the specific
data
from the observations
Syntax
The basic syntax for creating an if statement in SAS is:
IF (condition ) THEN result1;
23
ELSE result2;
Function Categories
Depending on their usage, the functions in SAS are categorized as follows.
Mathematical
Date and Time
24
Character
Truncation
Miscellaneous
Truncation Functions
These are the functions used to truncate numeric values.
Examples
The following SAS program shows the use of truncation functions.
data trunc_functions;
/* Nearest greatest integer */
ceil_ = CEIL(11.85);
/* Nearest greatest integer */
floor_ = FLOOR(11.85);
/* Integer portion of a number */
SAS
72
int_ = INT(32.41);
/* Round off to nearest value */
round_ = ROUND(5621.78);
run;
proc print data = trunc_functions noobs;
run;
a space, but there can be different types of delimiters also which SAS can
handle. Let’s
consider an ASCII file containing the employee data. We read this file using
the Infile statement available in SAS.
Following are the two prerequisites for merging the data sets:
input data sets must have at least one common variable to merge on.
input data sets must be sorted by the common variable(s) that will be
used to
merge on.
Syntax
The basic syntax for MERGE and BY statement in SAS is:
Subsetting Observations
In this method, we extract only few observations from the entire data set.
Syntax
We use PROC FREQ which keeps track of the observations selected for the
new data set.
The syntax for subsetting observations is:
IF Var Condition THEN DELETE ;
Following is the description of the parameters used:
Var is the name of the variable based on whose value the observations
will be
deleted using the specified condition.
Syntax
The basic syntax for sort operation in data set in SAS is:
PROC SORT DATA=original dataset OUT=Sorted dataset;
BY variable name;
Following is the description of the parameters used:
variable name is the column name on which the sorting happens.
Original dataset is the dataset name to be sorted.
Sorted dataset is the dataset name after it is sorted
Syntax
The basic syntax for using the ODS statement in SAS is:
ODS outputtype
PATH path name
FILE = Filename and Path
STYLE = StyleName
;
PROC some proc
;
ODS outputtype CLOSE;
27
PROC EXPORT
PROC EXPORT is a SAS in-built procedure. It is used to export the SAS data
sets for writing
the data into files of different formats.
Syntax
The basic syntax for writing the procedure in SAS is:
PROC EXPORT
DATA=libref.SAS data-set (SAS data-set-options)
OUTFILE="filename"
DBMS=identifier LABEL(REPLACE);
Syntax
The local variables are declared with the syntax shown below.
% LET (Macro Variable Name) = Value;
Macro Programs
Macro is a group of SAS statements that is referred by a name and to use it
in program
anywhere, using that name. It starts with a %MACRO statement and ends
with %MEND
statement.
Syntax
The local variables are declared with the syntax given below.
# Creating a Macro program.
%MACRO (Param1, Param2,….Paramn);
Macro Statements;
%MEND;
# Calling a Macro program.
%MacroName (Value1, Value2,…..Valuen);
29