Hi All,
Can you please help me to add up each observation value with its previous var value.
Suppose below is my data,
Sub var1
001 12
002 5
003 .
004 3
005 .
006 6
output1:
I want a results as below,
sub var1 var2
001 12 12
002 5 17
003 . 17
004 3 20
005 . 20
006 6 26
output2:
I want a results as below,
sub var1 var2
001 12 12
002 5 17
003 . .
004 3 20
005 . .
006 6 26
Please try
data have;
input Sub var1;
cards;
001 12
002 5
003 .
004 3
005 .
006 6
;
data want1(keep=sub var1 var3 rename=(var3=var2)) want2(keep=sub var1 var2);
set have ;
retain var3;
if var1 ne . then var3=sum(var1,var3);
if var1 ne . then var2=var3;
run;
Please try
data have;
input Sub var1;
cards;
001 12
002 5
003 .
004 3
005 .
006 6
;
data want1(keep=sub var1 var3 rename=(var3=var2)) want2(keep=sub var1 var2);
set have ;
retain var3;
if var1 ne . then var3=sum(var1,var3);
if var1 ne . then var2=var3;
run;
Just for fun with shorter syntax:
data have;
input Sub var1;
cards;
001 12
002 5
003 .
004 3
005 .
006 6
;
data
want1(drop = var2 rename=(var3=var2))
want2(drop = var3)
;
set have ;
var2 + var1;
var3 = ifn(var1,var2,var1);
run;
All the best
Bart
Hi @Ramya2
Here is another approach:
data want1;
set have;
var2 + var1;
run;
data want2;
set have;
_var + var1;
if not missing (var1) then var2 = _var;
drop _var;
run;
Best,
Try
data output1(drop=var2 rename=(cum_var1=var2)) output2(drop=cum_var1);
set have;
cum_var1+var1;
if var1 eq . then var2=.;
else var2=cum_var1;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.