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:
Final result should be:
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;
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;
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.