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

I have the following table. They are sorted by Group and Year.

What I would like to do is that: for each group, I would like to subtract the previous value from the previous row but keep the first observation value from each group as the same first value.

 

GroupYearRate
A20199.090909
A202027.27273
A202131.81818
B201822.72727
B201972.72727
B202095.45455
B2021100
C201831.81818
C201954.54545
C202068.18182
C202168.18182

 

Want:

GroupYearRaterate_2
A20199.0909099.090909
A202027.2727318.181821
A202131.818184.54545
B201822.7272722.72727
B201972.7272750
B202095.4545522.72728
B20211004.54545
C201831.8181831.81818
C201954.5454522.72727
C202068.1818213.63637
C202168.181820

 

I tried the following

data want; set test;

by group;

if first.group then rate_2 = rate;

else if rate_2 ..?

 

run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
SAS Super FREQ

The LAG function is you friend here, but make sure you always execute the LAG - never put a call to the LAG function in a conditional code block. Try this:

data have;
  infile datalines truncover;
  input Group:$1. Year:4. Rate:10.;
datalines;
A	2019	9.090909
A	2020	27.27273
A	2021	31.81818
B	2018	22.72727
B	2019	72.72727
B	2020	95.45455
B	2021	100
C	2018	31.81818
C	2019	54.54545
C	2020	68.18182
C	2021	68.18182
;

data want;
   set have;
   by group;
   /* ALWAYS execute the LAG function - never make it conditional */
   rate_2=sum(Rate,-lag1(rate));
   if first.group then rate_2=rate;
run;

proc print;
run;
Check out my Jedi SAS Tricks for SAS Users

View solution in original post

1 REPLY 1
SASJedi
SAS Super FREQ

The LAG function is you friend here, but make sure you always execute the LAG - never put a call to the LAG function in a conditional code block. Try this:

data have;
  infile datalines truncover;
  input Group:$1. Year:4. Rate:10.;
datalines;
A	2019	9.090909
A	2020	27.27273
A	2021	31.81818
B	2018	22.72727
B	2019	72.72727
B	2020	95.45455
B	2021	100
C	2018	31.81818
C	2019	54.54545
C	2020	68.18182
C	2021	68.18182
;

data want;
   set have;
   by group;
   /* ALWAYS execute the LAG function - never make it conditional */
   rate_2=sum(Rate,-lag1(rate));
   if first.group then rate_2=rate;
run;

proc print;
run;
Check out my Jedi SAS Tricks for SAS Users

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 2294 views
  • 0 likes
  • 2 in conversation