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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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