BookmarkSubscribeRSS Feed
RNscientist
Calcite | Level 5

Hello.

 

I am trying to sum 5 columns to get a total score for each participant (row); however, I don't want SAS to sum rows with missing data, but I also don't want to delete those participants as I have data for them on a different variable.

 

So far I have tried 

 

data want;
set have;
sum_W1= sum(var1, var2, var3, var4, var5);

run;

 

however this sums across the row even if I'm missing data in 4 out of the 5 columns. 

 

For instance 

var 1  var 2 var 3  var 4   var 5

.            .            3        .             .

 

This is giving me a total score of 3 but I want it to read . 

 

Thanks for any help! 

3 REPLIES 3
Astounding
PROC Star

This might suit what you are asking for:

if n(of var1-var5) = 5 then sum_W1=sum(of var1-var5);

Make sure you include that key word "of".  Without it, SAS will compute var1 minus var5.

RNscientist
Calcite | Level 5

YES! Thank you so much!!! 

ballardw
Super User

Admittedly long to type for many variables but the + operator will return missing if any of the variables are missing:

 

sum_W1= var1+ var2+ var3+ var4+ var5;

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
  • 483 views
  • 2 likes
  • 3 in conversation