A logic error is a common topic in the new SAS base specialist certification exam.
It is the most difficult error to spot since there is no error message that warns you about the issue.
You will have to check if the desired results are achieved based on the initial objectives.
In this section, we will look at two examples of logic errors and how we can identify and correct them.
Let's first look at the SASHELP.CARS data set:
The SASHELP.CARS data set contains a list of cars and their respective models, MSRP...etc.
We will now split the cars into three price categories:
Below is the code:
data cars; set sashelp.cars; if msrp <= 30000 then price = "L"; if 30000 < msrp <= 70000 then price = "M"; else price = "H"; run;
keep make model msrp price;
The code seems fine:
The PRICE column is created as the last column of the data set:
The SAS log indicates no issue:
What could go wrong?
We will first run a Proc Freq and check if the pricing group assignment is correct.
proc freq data=cars; table price; run;
It turns out that all of the cars are grouped into the 'M' and 'H' categories
There are no cars in the 'L' category:
Do we not have any cars below 30,000?
data issues; set cars; where msrp <= 30000; run;
We filter the data set and keep only the car models that are below 30,000:
We have quite a few cars that are below 30,000 MSRP. They are all grouped into the 'H' category.
This is an issue that we have to fix.
Another way to identify the issue is to use the PUTLOG statement.
Let's look at an example where we added a PUTLOG statement within an IF condition:
data cars; set sashelp.cars; if msrp <= 30000 then price = "L"; if 30000 < msrp <= 70000 then price = "M"; else price = "H"; if msrp <=30000 then do; putlog msrp= price=; end; keep make model msrp price; run;
The PUTLOG statement is placed within an IF-statement after we define the PRICE variable.
It writes the MSRP and PRICE values to the SAS log whenever the MSRP is at or below 30,000.
You will see the notes below on the SAS log after you run the code:
We now know for sure there are some issues with how we define the PRICE column in the IF-THEN statement.
Let's go back to the code.
Can you spot any issue with the if-then statement?
Yes, we are missing an ELSE keyword in the second IF-statement:
Since we don't have the ELSE keyword in the second IF statement, SAS treats the first IF statement and the second IF statement as two independent statements:
In the second IF-THEN-ELSE statement, SAS groups the cars into 'M' if the MSRP is between 30,000 to 70,000.
Everything else is grouped into 'H'.
As a result, the grouping done in the first IF statement is overwritten.
That's the reason why no cars are grouped into the 'L' category, even for cars that are below 30,000 MSRP.
Once you identify the issues, fixing the program becomes much easier.
You just need to include the ELSE keyword in the if-then statement.
data cars; set sashelp.cars; if msrp <= 30000 then price = "L"; else if 30000 < msrp <= 70000 then price = "M"; else price = "H"; keep make model msrp price; run; proc freq data=cars; table price; run;
The 'L' category can now be found in the frequency table:
Copy and run the code below in SAS:
The RECORDS data set contains the homework, midterm and final scores for 10 students.
Below is the code the teacher used to calculate the final score for each student:
data total; set records; final_score = sum(homework + midterm + final); run;
If the scores for homework, midterm or final is missing, it should be treated as 0.
The FINAL_SCORE should sum up all three scores and give the student the final results for this particular subject.
Did you find any issue with the code above?
Need some help?
Is there any student missing a final score?
** Running the code given **;
final_score = sum(homework + midterm + final);
** The final scores for some students are missing **;
where final_score = .;
** The SUM function is corrected **;
final_score = sum(homework, midterm, final);