BookmarkSubscribeRSS Feed
a_zacMD
Obsidian | Level 7

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.

 

CountScore 1Score 2Score 3
1124
2444
3231
4121
10 REPLIES 10
jimbarbour
Meteorite | Level 14

I think Total = SUM(of Score_1 - Score3) should do it.

 

Jim

a_zacMD
Obsidian | Level 7
I was hoping something like this might work:
ABC_Score = Score_1 + Score_2 + Score_3;
end;
jimbarbour
Meteorite | Level 14

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

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
jimbarbour
Meteorite | Level 14

@a_zacMD,

 

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

a_zacMD
Obsidian | Level 7

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 

jimbarbour
Meteorite | Level 14

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.

jimbarbour_0-1602179121345.png

 

Jim

a_zacMD
Obsidian | Level 7
I found this string of suggestions on google groups (here https://groups.google.com/g/comp.soft-sys.sas/c/9RxJi93icuw?pli=1)
And using a double hyphen worked!!!

Thank you so much for all the help! I am sure I will be back with more questions and I hope to be able to provide support to other community members.
jimbarbour
Meteorite | Level 14

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

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 10 replies
  • 1775 views
  • 7 likes
  • 4 in conversation