I have a result table where there will always be two dates, I would like to subtract hip and not from each other, which is the more recent date than the previous one, and make a diffrence row with the difference. Unfortunately, I don't know how
my input :
hip | date | ubez | |
124545 | 2021-09-30 | 119348 | |
123981 | 2021-10-31 | 119419 |
expect output
hip | date | ubez | |
124545 | 2021-09-30 | 119348 | |
123981 | 2021-10-31 | 119419 | |
Diffrence | 564 | -71 |
That's the whole problem: a data set with two rows? Or is this a more general problem where you have lots of rows, paired into groups of two?
data have;
input hip date :yymmdd10. ubez;
cards;
124545 2021-09-30 119348
123981 2021-10-31 119419
;
data want;
set have end=eof;
prev_hip=lag(hip);
prev_ubez=lag(ubez);
prev_date=lag(date);
output;
if eof then do;
if date<prev_date then do;
hip=hip-prev_hip;
ubez=ubez-prev_ubez;
end;
else do;
hip=prev_hip-hip;
ubez=prev_ubez-ubez;
end;
call missing(date);
output;
end;
drop prev_: direction;
format date yymmddd10.;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.