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;
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.