BookmarkSubscribeRSS Feed
ThetaIchnaea
Fluorite | Level 6

I'm doing an introductory course in SAS, and when I copy the code in one of the videos, I get different results. I have no idea what is going wrong here.

 

data sales;
input name$ sales1-sales4 total;
total = sales1 + sales2 + sales3 + sales4;

datalines;
Greg 10 2 40 0
John 15 5 10 100
Lisa 50 10 15 50
Mark 20 0 5 20
run;

The output should give the four lines with six colums: name, four sales values, and the total per person. When I run this code in SAS Studio (University Edition, latest version), I get only result for Greg and Lisa.

 

NOTE: Invalid data for total in line 79 1-4.
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
79 John 15 5 10 100
name=Greg sales1=10 sales2=2 sales3=40 sales4=0 total=52 _ERROR_=1 _N_=1
NOTE: Invalid data for total in line 81 1-4.
81 Mark 20 0 5 20
name=Lisa sales1=50 sales2=10 sales3=15 sales4=50 total=125 _ERROR_=1 _N_=2

 I don't see what is wrong with the data and what causes this error. I've created a text file with the four datalines, and used that with an INFILE command, but that didn't make any difference. Can you explain or help me out?

5 REPLIES 5
ed_sas_member
Meteorite | Level 14

Hi @ThetaIchnaea 

 

Actually, the TOTAL variable is a computed one.

It is not present in the input data.

So you need to remove it from the input statement:

data sales;
input name$ sales1-sales4 ;
total = sales1 + sales2 + sales3 + sales4;

datalines;
Greg 10 2 40 0
John 15 5 10 100
Lisa 50 10 15 50
Mark 20 0 5 20
;
run;

Best,

ThetaIchnaea
Fluorite | Level 6
Thanks! That fixed the problem.
Reeza
Super User
NOTE: Invalid data for total in line 79 1-4.
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
79 John 15 5 10 100
 
The error tells you SAS is trying to read in data for a variable called total and when to the next line (flowover is the default in INFILE statement) to try and find that value. Since it's a calculated column it doesn't need to be in the INPUT statement. I highlighted the data in the NOTE that illustrates how you can determine what's happening.
 
ThetaIchnaea
Fluorite | Level 6
So it means that with INFILE it reads Greg, then sales 1-4, and then tries to read total. There is no total on the first line, so it goes to the second line, where it finds John, which results in an error because it's character data and not a value. That's why it doesn't display the John line? Is that how this works?

Knowing that TOTAL should be removed from INPUT is useful, but knowing how to read the log is even better.
Reeza
Super User

@ThetaIchnaea wrote:
So it means that with INFILE it reads Greg, then sales 1-4, and then tries to read total. There is no total on the first line, so it goes to the second line, where it finds John, which results in an error because it's character data and not a value. That's why it doesn't display the John line? Is that how this works?


Yes, that's correct. Another possible correction could be to add the TRUNCOVER option to the INFILE statement which would tell SAS not to go to a new line when it doesn't find the total value.  Then it should stay empty and be filled in with the calculation in the next step. You will likely still get Notes in the log with this method though but it's worth noting the option. IMO the default for INFILE should be changed to TRUNCOVER due to how we store files today but SAS will likely not change this due to backwards compatibility issues. 

 

infile cards truncover;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 613 views
  • 2 likes
  • 3 in conversation