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;
@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));
productID seems to be a character variable. convert it to numeric using the INPUT function 🙂
http://sasnrd.com/convert-character-numeric/
@PeterClemmensen wrote:
productID seems to be a character variable. convert it to numeric using the INPUT function 🙂
I don't think that will work.
@data_null__ wrote:
@PeterClemmensen wrote:
productID seems to be a character variable. convert it to numeric using the INPUT function 🙂
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.
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.
@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.
@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);
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.
@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));
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.
Thanks for everone's prompt replies and great suggestions. It works.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.