Using SAS Enterprise Guide, I want to sum multiple numeric values and create a new "total" variable. Ideally I would like to create something like I feel like this should be relatively straightforward, but I am missing something. Thank you in advance.
Count | Score 1 | Score 2 | Score 3 |
1 | 1 | 2 | 4 |
2 | 4 | 4 | 4 |
3 | 2 | 3 | 1 |
4 | 1 | 2 | 1 |
I think Total = SUM(of Score_1 - Score3) should do it.
Jim
It should, but SUM will handle missing values where as a simple formula as I recall will not.
Let me mock something up for you. Give me a minute.
Jim
@a_zacMD wrote:
I was hoping something like this might work:
ABC_Score = Score_1 + Score_2 + Score_3;
end;
Give it a try! See what happens.
Consider the below code and the results posted below it. In the results, Total 1 is from Score_1 + Score_2 + Score_3. Total 2 is from SUM(of Score_1 - Score_3).
For datalines 1 through 4, all is well, and Totals 1 and 2 are the same. But what about datalines 5 - 7? Notice that there are missing values in the data. Suddenly, Total 1 is also missing. Why? Well, recall that a mathematical operation on a missing value results in missing. SUM on the other hand is a bit more sophisticated and just, in essence, skips missing values. Hence, functions are generally to be preferred over mathematical operations whenever missing values may be at play.
DATA Have;
INFILE Datalines DSD DLM='09'x MISSOVER;
INPUT Count $
Score_1
Score_2
Score_3
;
Total1 = Score_1 + Score_2 + Score_3;
Total2 = SUM(of Score_1 - Score_3);
Datalines;
1 1 2 4
2 4 4 4
3 2 3 1
4 1 2 1
5 . 9 8
6 7 . 12
7 1
;
RUN;
Results:
Count Score_1 Score_2 Score_3 Total1 Total2 1 1 2 4 7 7 2 4 4 4 12 12 3 2 3 1 6 6 4 1 2 1 4 4 5 9 8 17 6 7 12 19 7 1 1
Jim
Thank you so much for the feedback!
I did try the
Total2 = SUM(of Score_1 - Score_3);
approach but got an error saying
ERROR: Missing numeric suffix on a numbered variable list (Score_1 -Score_3).
ERROR: 71-185 The SUM function call does not have enough arguments.
I am going to google the errors. But I was thinking maybe I should spell out the variables? All the best, a
If you use the - notation, make sure you use "of" before Score_1 otherwise SAS thinks you're trying to subtract Score_3 from Score_1.
Total2 = SUM(of Score_1 - Score_3);
Alternatively, list the variables name by name.
Total2 = SUM(Score_1 Score_2 Score_3);
If you've already got "of" in place, then post your code and your log, and let's have a look. The "Insert Code" is really good for posting a log.
Jim
Excellent!
Both single hyphen notation and double hyphen notation are helpful.
You probably know this now, but:
Single hyphen works with variables that have a number at the end. Var1, Var2, Var3, ... Var99. can be referred to as Var1 - Var99.
Double hyphen works with variables that are contiguous in the Program Data Vector. If you run a Proc Contents and use the VARNUM option, the list will be in variable order (not alphabetical order). If you want a section (or all) of the variables, you can list the first and the last with a -- in between, and SAS will process all variables in between the first specified and the last. The variables must be contiguous in order for this to work.
Jim
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.