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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.