BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

Appreciate if someone of you help me understand the difference between the following two sum functions. I just wanted to know the difference between the two.

 

 

sum ('7101100000'n ,
         '7101100010'n ,
         '7101101000'n 
         ) as PREM_AMT
SUM('6131112300'n) AS SUM_of_6131112300

 

8 REPLIES 8
Babloo
Rhodochrosite | Level 12

Could you please clarify the meaning of "sums one variable across observations"?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

VAR1   VAR2   VAR3

abc      def        ghi

abc      def        ghi

 

Sum across observations means the value from one row of abc + def + ghi

Sum column means all from one column abc + abc

Babloo
Rhodochrosite | Level 12

When you say "Sum column means all from one column abc + abc", I will be getting the same value for all the observations if new variable VAR4 is being created?

Kurt_Bremser
Super User

@Babloo wrote:

Could you please clarify the meaning of "sums one variable across observations"?


Should have said "over several observations", as across might be misleading.

 

Also see this result of a google search for "sas sql summary functions".

Babloo
Rhodochrosite | Level 12

When you say "over several observations" whether I will be getting the same value for all the observations if new variable VAR4 is being created?

Kurt_Bremser
Super User

Just try it.

 

Run this code:

data have;
input x y;
cards;
1 2
3 4
5 6
;
run;

proc sql;
create table want1 as
select sum(x,y) as z
from have;
create table want2 as
select sum(x) as x, sum(y) as y
from have;
quit;

proc print data=want1 noobs;
run;
proc print data=want2 noobs;
run;

and look at the results.

ballardw
Super User

History aside:

SAS started in 1966 or thereabouts and started creating data step programming.

SQL started a few years later.

 

Each had their own idea of "sum" because of how the two packages were used.

 

Years later SAS incorporated the Proc SQL to have many of the features of SQL and hence used the SQL core features like SUM as an aggregate function across rows so existing SQL programs were easier to convert (and to not confuse SQL programmers).

 

Note that SUM in proc means, summary, tabulate, report and print are much like the aggregate Sum in proc sql if that helps any.

 

So this issue of which is which comes up from occasional users of both paths.

The computer world if rife with such "similarities". I learned one OS that used CAT for catalog (listing of files), UNIX didn't like that at all when I tried that when I was learning the little I know of Unix. {For non-Unix programmers CAT concatenates files, very different behavior}.

 

Attention to detail and syntax guides are your friends.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 8 replies
  • 858 views
  • 4 likes
  • 4 in conversation