BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
dustychair
Pyrite | Level 9

Hi all,

I am trying to sum some variables which have scores 0-1-2 and characters 'Z' or 'M'. I want to sum numbers and ignore those character responses. Any ideas how i can do that?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Make a NUMERIC variable.  Now you can do any arithmetic you want with the variable.  

There is no need to define a special INFORMAT. Use the MISSING statement to tell SAS which single letter text strings to interpret as representing the corresponding special missing numeric values.  

missing M Z ;
data have;
  input var ;
cards;
1
0
2
M
Z
;
proc print;
run;
proc means n min max mean;
run;

Tom_0-1658415932908.png

 

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

Convert the variables to numeric; use the special missing values .M and .Z for the characters.

The SUM function will then work correctly, as will procedures like MEANS.

s_lassen
Meteorite | Level 14

You can also create in informat and use that in SQL:

data have;
input ID score $;
cards;
1 1
1 3
1 M
2 Z
2 2
2 2
2 3
;run;

proc format; invalue score '1'=1 '2'=2 '3'=3 'M'=.M 'Z'=.Z
other=_ERROR_ ; run; proc sql; create table want as select id,sum(input(score,score.)) as score from have group by id; quit;

Of course, you could also use the informat when reading the data in the first place, you can then use PROC SUMMARY etc. on the score variable, which is now numeric: 

data have;
  input ID score score.;
cards;
1 1
1 3
1 M
2 Z
2 2
2 2
2 3
;run;
Tom
Super User Tom
Super User

Make a NUMERIC variable.  Now you can do any arithmetic you want with the variable.  

There is no need to define a special INFORMAT. Use the MISSING statement to tell SAS which single letter text strings to interpret as representing the corresponding special missing numeric values.  

missing M Z ;
data have;
  input var ;
cards;
1
0
2
M
Z
;
proc print;
run;
proc means n min max mean;
run;

Tom_0-1658415932908.png

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 563 views
  • 1 like
  • 4 in conversation