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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.