BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10
data ds;
input  x;
datalines;
1
2
3
4
5
-6
-7
-8
-9
;
run;

data t;
set ds;
if x>0 then do 
y=x+x	; 
if x<0 then do
y=x+x;
end;
end;
if x ge 0 then output;
if y le 0	then output;
run;

Here i am trying sum first five numbers and also do sum negivtive numbers in flag variable

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data ds;
input  x;
datalines;
1
2
3
4
5
-6
-7
-8
-9
;
run;
/*SQL*/
proc sql;
create table want as
select sum(x) as s
from ds
group by sign(x);
quit;

/*Datastep Hash method*/
data want;
 if _n_=1 then do;
   dcl hash H () ;
   h.definekey  ("_n_") ;
   h.definedata ("sum") ;
   h.definedone () ;
 end;
 set ds end=z;
 if h.find(key:sign(x))=0 then sum=sum(sum,x);
 else sum=x;
 h.replace(key:sign(x),data:sum);
 if z;
 dcl hiter hi('h');
 do while(hi.next()=0);
  output;
 end;
 drop x;
run;
  

 


data want;
set ds end=z;
if _n_>1 and sign(x) ne lag(sign(x)) then do;
sum=0;
end;
sum+x;
run;

View solution in original post

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

What does your desired result look like here?

BrahmanandaRao
Lapis Lazuli | Level 10

HI Draycut

 

i want first 1 to 5 cumulative and also -6 to -9 values cumulative sum 

 

ed_sas_member
Meteorite | Level 14

Hi @BrahmanandaRao 

 

Here is an alternative method:

 

data ds;
	input x;
	if x>0 then	group=1;
	else if x<0 then group=2;
	datalines;
1
2
3
4
5
-6
-7
-8
-9
;
run;

proc means data=ds sum noprint;
	var x;
	by group;
	output out=t (drop=_type_ _freq_) sum=grand_sum;
run;
Jagadishkatam
Amethyst | Level 16

please try

 

data ds;
input  x;
if x gt 0 then grp=1;
else grp=2;
datalines;
1
2
3
4
5
-6
-7
-8
-9
;
run;

proc sql;
create table want as select sum(x) as sum, grp from ds group by grp;
quit;
Thanks,
Jag
novinosrin
Tourmaline | Level 20

data ds;
input  x;
datalines;
1
2
3
4
5
-6
-7
-8
-9
;
run;
/*SQL*/
proc sql;
create table want as
select sum(x) as s
from ds
group by sign(x);
quit;

/*Datastep Hash method*/
data want;
 if _n_=1 then do;
   dcl hash H () ;
   h.definekey  ("_n_") ;
   h.definedata ("sum") ;
   h.definedone () ;
 end;
 set ds end=z;
 if h.find(key:sign(x))=0 then sum=sum(sum,x);
 else sum=x;
 h.replace(key:sign(x),data:sum);
 if z;
 dcl hiter hi('h');
 do while(hi.next()=0);
  output;
 end;
 drop x;
run;
  

 


data want;
set ds end=z;
if _n_>1 and sign(x) ne lag(sign(x)) then do;
sum=0;
end;
sum+x;
run;
Astounding
PROC Star

Somehow this looks like using a sledgehammer to swat a fly.  This should be pretty close to the mark:

 

data want;
   set have end=done;
   if x > 0 then positive_total + x;
   else negative_total + x;
   if done;
run;
DanielLangley
Quartz | Level 8
Does this not need a retain statement?
Kurt_Bremser
Super User

@Astounding wrote:

Somehow this looks like using a sledgehammer to swat a fly.  This should be pretty close to the mark:

 

data want;
   set have end=done;
   if x > 0 then positive_total + x;
   else negative_total + x;
   if done;
run;

This meets Saint-Exupery's definition of perfection.

Astounding
PROC Star
No, this syntax automatically retains:

Varname + value;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 9 replies
  • 2466 views
  • 2 likes
  • 8 in conversation