BookmarkSubscribeRSS Feed
bollurajkumar
Fluorite | Level 6
Hai all ,
I have a data like this.

A. B
World 25
India 30
USA 40
Pak 70

I want sum of total into world b column
Like
World 165
India 30
USA 40
Pak 70
Thanks in adv
8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

In a data set or report?

PeterClemmensen
Tourmaline | Level 20

In either case, you can do something like this

 

data have;
input A $ B;
datalines;
World 25
India 30
USA 40
Pak 70
;

proc sql;
   create table want as
   select 'World' as A, sum(B) as B from have
   union
   select * from have where A ne 'World'
   order by B descending;
quit;
Kurt_Bremser
Super User

I had a slightly different take on the SQL:

proc sql;
create table want as
select
  a,
  case
    when a = 'World' then (select sum(b) from have)
    else b
  end as b
from have;
quit;
singhsahab
Lapis Lazuli | Level 10

Hi,

 

data have;
input  A $ B;
datalines;
World 25
India 30
USA 40
Pak 70
;
run;

proc report data=have;
column a b;
define a/order descending;
define a/order;
rbreak before /summarize;
run;

DATA WANT;
SET HAVE;
RETAIN S 0;
S=S+B;
if a='World' then call sortn(s,b);
else call sortn(b,s);
drop s;
RUN;

Thanks 🙂 

Jagadishkatam
Amethyst | Level 16

By data step

 

data have;
input  A $ B;
datalines;
World 25
India 30
USA 40
Pak 70
;
run;

data want;
set have;
if a='World' then sort=1;
else sort=0;
call sortn(sort,b);
run;

proc sort data=want;
by sort;
run;

DATA WANT2;
SET want;
by sort;
S+B;
if a='World' then b=s;
else b=b;
drop s;
RUN;

proc sort data=want2;
by sort;
run;
Thanks,
Jag
novinosrin
Tourmaline | Level 20
data have;
input A $ B;
datalines;
World 25
India 30
USA 40
Pak 70
;


data _null_ ;
if _n_=1 then do;
   declare hash H (ordered:'a') ;
   h.definekey  ("_n_") ;
   h.definedata ("a", "b") ;
   h.definedone () ;
   end;
set have end=lr;
rc=h.add();
k+b;
if lr;
_n_=1;
h.find();
b=k;
h.replace();
h.output(dataset:'want');
run;
novinosrin
Tourmaline | Level 20

More fun

 

data have;
input A $ B;
datalines;
World 25
India 30
USA 40
Pak 70
;
data _null_ ;
if _n_=1 then do;
   declare hash H (ordered:'a') ;
   h.definekey  ("_n_") ;
   h.definedata ("a", "b") ;
   h.definedone () ;
   end;
do _n_=nobs to 1 by -1;
set have nobs=nobs point=_n_;
k+b;
if _n_=1 then b=k;
rc=h.add();
end;
h.output(dataset:'want');
stop;
run;
novinosrin
Tourmaline | Level 20

And more fun:

 


data have;
input A $ B;
datalines;
World 25
India 30
USA 40
Pak 70
;


proc sql;
create table want as
select a, ifn(monotonic()=1,sum(b),b) as b
from have
order by monotonic();
quit;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2395 views
  • 2 likes
  • 6 in conversation