5 Mistakes to Avoid in SAS
You have mastered Base SAS and are all set to appear for its Certification exam. Best of luck! Wait! Are you prepared enough though? Read on to learn 5 trivial mistakes that could tank your chances of cracking the exam!
What do you think is the length of the variable TYPE in the code-set below?
if code=’1′ then Type=’Fixed’;
length Type $ 10;
If you look at the statement before run; the length is 10 characters, right?
Remember! The length of a new variable is determined by its first reference in the DATA step. In this case, the length of TYPE is determined by the value ‘Fixed’. The LENGTH statement is in the wrong place. It will work only if it is placed before any other reference to the variable in the DATA step. The LENGTH statement cannot alter the length of an existing variable.
Can you predict the output of the following program?
if A = 4 or 5 then Found = ‘Yes’;
else Found = ‘No’;
title “Listing of Compare”;
proc print data=compare noobs;
You might expect that the above program would result in following output:
Listing of Compare
But the correct output is:
Listing of Compare
Surprised? This is because, in SAS, any value other than 0 or missing is true. Therefore, 5 is evaluated as true and the statement A = 4 or 5 is always true.
If the dataset NORTH had 3 observations and SOUTH had 6 observations, how many observations would the dataset EMP have in the following code:
set north(in = a) south(in=b);
if a and b;
This looks like a simple case of vertical stacking and hence you would expect that the dataset emp has 9 observations, right?
While stacking the first dataset, IN variable is 1 for the first dataset and 0 for the second. While stacking the second dataset, IN variable is 1 for second dataset and 0 for first dataset. It is never 1 for both together. Hence the output dataset emp would have zero or no observations.
You know that column input is appropriate only in the following situations.
Now what do you think would be the value of the variable BREADTH in the output dataset below?
input @1 length 2. @4 breadth 2;
You say “Breadth is 95, obviously!”. Right?
The correct answer is 2. This is a case of column input. The ‘2’ after BREADTH in input statement specifies the starting column from which BREADTH is to be read and not the format. Remember, there is no ‘dot’ after 2.
Look at the program below:
proc print data = new;
In the above program, an observation is first read from the SHORT data set and the observation is written out to the NEW data set. Then an observation is read from the LONG data set, and is written to the NEW data set. You expect that this would continue until all the observations from both the datasets are read. So, the dataset NEW would have 1,3,2,4,5,6. Right?
The dataset SHORT has only 2 observations 1 and 2. It is a shorter dataset. As soon as the end of file on data set SHORT is encountered, it signals an end to the DATA step, with the result that data set NEW has only four observations, with values of x equal to 1, 3, 2, and 4.
So these are some of the common mistakes you can make while executing a SAS code. Make sure to avoid these!
“Sometimes, little things make a big difference…”
– Nino Varsimashvili
RECOMMENDED FOR YOU
Sign Up For A Free Ebook & Exclusive Updates!