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-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!

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
  • 423 views
  • 1 like
  • 4 in conversation