BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dht115
Calcite | Level 5

Hello, 

 

I want to sum by variable and use that sum value to calculate additional variable. 

In following dataset, I need to do sum for var1, which is 10. I need to add this sum value to create var2. 

var2 = sum + var1

 

Input dataset: 

dht115_0-1642022588538.png

 Final result should be: 

dht115_1-1642022724543.png

 

 

DATA have;
	input group $ var1;
	datalines;
	A 1
	B 2
	C 3
	D 4
;
RUN;

PROC SUMMARY DATA=have;
   var var1;
	output out=have1 (DROP = _TYPE_ _FREQ_) sum=;
RUN;

DATA want;
	set have1;
	rename var1 = var2;
RUN;
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
PROC SUMMARY DATA=have;
   var var1;
   output out=have1 (DROP = _TYPE_ _FREQ_) sum=sum_values;
RUN;
data want; 
    if _n_=1 then set have1;
    set have;
    var1=var1+sum_values;
run;
--
Paige Miller

View solution in original post

1 REPLY 1
PaigeMiller
Diamond | Level 26
PROC SUMMARY DATA=have;
   var var1;
   output out=have1 (DROP = _TYPE_ _FREQ_) sum=sum_values;
RUN;
data want; 
    if _n_=1 then set have1;
    set have;
    var1=var1+sum_values;
run;
--
Paige Miller

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
  • 1 reply
  • 842 views
  • 0 likes
  • 2 in conversation