DATA Step, Macro, Functions and more

Syntax error in numeric expression?

Accepted Solution Solved
Reply
Super Contributor
Posts: 319
Accepted Solution

Syntax error in numeric expression?

Hello:

 

I have a code below,  and I received an error message from log.  Please advice how to fix it, thanks.

 

data want;

set have;

if stateid ^=. then productID+1;

ERROR 400-185: The SUM statement requires numeric expression.

run; 


Accepted Solutions
Solution
‎08-08-2017 03:34 PM
Super User
Super User
Posts: 7,083

Re: Syntax error in numeric expression?


ybz12003 wrote:

I have tried both of the codes, none of them was working.  All of the StateID are numeric.

 

925 data allstaterename1; set allstaterename;

926 If mrs_stateid ^=. then momID+1;

-

400

ERROR 400-185: The SUM statement requires numeric expression.

927 /* If not missing (nad_stateid) then childID+2;*/ run;

 

 

928 data allstaterename1; set allstaterename;

929 /*If mrs_stateid ^=. then momID+1;*/

930 If not missing (nad_stateid) then childID+2; run;

-

400

ERROR 400-185: The SUM statement requires numeric expression.

 


You cannot use the SUM statement like:

momID+1;
childID+1;

with character variables.

So either use a different variable name.

momID_number+1;
childID_number+1;

Or use the DROP= or RENAME= dataset options on the input dataset to get the old variable out of the way.

 set allstaterename(drop=momID);
 set allstaterename(rename=(momID=old_momID));

View solution in original post


All Replies
PROC Star
Posts: 768

Re: Syntax error in numeric expression?

[ Edited ]

productID seems to be a character variable. convert it to numeric using the INPUT function Smiley Happy

 

http://sasnrd.com/convert-character-numeric/

 

Respected Advisor
Posts: 3,799

Re: Syntax error in numeric expression?


draycut wrote:

productID seems to be a character variable. convert it to numeric using the INPUT function Smiley Happy


 

I don't think that will work.

Trusted Advisor
Posts: 1,934

Re: Syntax error in numeric expression?

Posted in reply to data_null__

data_null__ wrote:

draycut wrote:

productID seems to be a character variable. convert it to numeric using the INPUT function Smiley Happy


 

I don't think that will work.


Of course, the original poster has not told us what ProductID contains, but it is highly suspicious from a logical point of view that adding one to productID makes any sense at all.

Super Contributor
Posts: 319

Re: Syntax error in numeric expression?

There is no ProductID column in the table 'have',   I would like to create one when the StateID is not blank, and the new generated column ProductID is the same as StateID.  Thanks.

Super User
Posts: 11,343

Re: Syntax error in numeric expression?


ybz12003 wrote:

There is no ProductID column in the table 'have',   I would like to create one when the StateID is not blank, and the new generated column ProductID is the same as StateID.  Thanks.


That sounds like:

 

If not missing(stateid) then ProductId=StateId;

 

Again, showing input data and desired output may clarify what you are actually attempting.

Super User
Super User
Posts: 7,083

Re: Syntax error in numeric expression?

[ Edited ]

ybz12003 wrote:

There is no ProductID column in the table 'have',   I would like to create one when the StateID is not blank, and the new generated column ProductID is the same as StateID.  Thanks.


If that is true the the error is for the other variable in your statement.

if stateid ^=. then productID+1;

Your code is treating STATEID as if it was a number, so SAS tried to convert the value to a number.  

Use the MISSING() function which does not care what type of variable you give it.

If not missing(stateid) then ProductId+1;

But if you want PRODUCTID to be the same as STATEID it seems you just want.

 

ProductId = coalescec(stateid,ProductId);
Super Contributor
Posts: 319

Re: Syntax error in numeric expression?

I have tried both of the codes, none of them was working.  All of the StateID are numeric.

 

925 data allstaterename1; set allstaterename;

926 If mrs_stateid ^=. then momID+1;

-

400

ERROR 400-185: The SUM statement requires numeric expression.

927 /* If not missing (nad_stateid) then childID+2;*/ run;

 

 

928 data allstaterename1; set allstaterename;

929 /*If mrs_stateid ^=. then momID+1;*/

930 If not missing (nad_stateid) then childID+2; run;

-

400

ERROR 400-185: The SUM statement requires numeric expression.

 

Solution
‎08-08-2017 03:34 PM
Super User
Super User
Posts: 7,083

Re: Syntax error in numeric expression?


ybz12003 wrote:

I have tried both of the codes, none of them was working.  All of the StateID are numeric.

 

925 data allstaterename1; set allstaterename;

926 If mrs_stateid ^=. then momID+1;

-

400

ERROR 400-185: The SUM statement requires numeric expression.

927 /* If not missing (nad_stateid) then childID+2;*/ run;

 

 

928 data allstaterename1; set allstaterename;

929 /*If mrs_stateid ^=. then momID+1;*/

930 If not missing (nad_stateid) then childID+2; run;

-

400

ERROR 400-185: The SUM statement requires numeric expression.

 


You cannot use the SUM statement like:

momID+1;
childID+1;

with character variables.

So either use a different variable name.

momID_number+1;
childID_number+1;

Or use the DROP= or RENAME= dataset options on the input dataset to get the old variable out of the way.

 set allstaterename(drop=momID);
 set allstaterename(rename=(momID=old_momID));
Trusted Advisor
Posts: 1,934

Re: Syntax error in numeric expression?

Great, you have tried the code suggested.

 

Now, let's go further ... it was also suggested that you SHOW US YOUR DATA (or at least, a relevant portion of your data). I think that's the only way we are going to get you an answer.

Super Contributor
Posts: 319

Re: Syntax error in numeric expression?

Thanks for everone's prompt replies and great suggestions.  It works. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 157 views
  • 3 likes
  • 6 in conversation