BookmarkSubscribeRSS Feed
Palucci
Fluorite | Level 6

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 :

hipdateubez 
1245452021-09-30119348 
1239812021-10-31119419 

expect output

 hipdateubez
 1245452021-09-30119348
 1239812021-10-31119419
Diffrence564 -71
3 REPLIES 3
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
Palucci
Fluorite | Level 6
data set with two lines, always subtracting the current
PaigeMiller
Diamond | Level 26
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;
--
Paige Miller
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
  • 3 replies
  • 904 views
  • 0 likes
  • 2 in conversation