code | date | Rtr | exp | new |
20 | 2011 | 0.115225 | 0.114746 | |
20 | 2012 | 0.101424 | 0.11639 | 0.231615 |
20 | 2013 | 0.114746 | 0.055248 | 0.156672 |
23 | 2011 | 0.11639 | 0.094931 | |
23 | 2012 | 0.055248 | 0.173508 | 0.289898 |
23 | 2013 | 0.094931 | 0.190566 | 0.245813 |
55 | 2011 | 0.029143 | 0.073573 | |
55 | 2012 | 0.060995 | 0.085915 | 0.115057 |
55 | 2013 | 0.069918 | 0.075433 | 0.136429 |
I want to add rtr in 2011 with exp in 2012. So my result will be equal to as shown in New column. It is adding the lag value of rtr with the current value of exp. The result should be grouped by code. Please help.
You can do like this
data have;
input code date Rtr exp;
datalines;
20 2011 0.115225 0.114746
20 2012 0.101424 0.11639
20 2013 0.114746 0.055248
23 2011 0.11639 0.094931
23 2012 0.055248 0.173508
23 2013 0.094931 0.190566
55 2011 0.029143 0.073573
55 2012 0.060995 0.085915
55 2013 0.069918 0.075433
;
data want;
set have;
by code;
lag_Rtr=lag1(Rtr);
new=sum(lag_Rtr, exp);
if first.code then new=.;
run;
You can do like this
data have;
input code date Rtr exp;
datalines;
20 2011 0.115225 0.114746
20 2012 0.101424 0.11639
20 2013 0.114746 0.055248
23 2011 0.11639 0.094931
23 2012 0.055248 0.173508
23 2013 0.094931 0.190566
55 2011 0.029143 0.073573
55 2012 0.060995 0.085915
55 2013 0.069918 0.075433
;
data want;
set have;
by code;
lag_Rtr=lag1(Rtr);
new=sum(lag_Rtr, exp);
if first.code then new=.;
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.