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

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 445 views
  • 0 likes
  • 2 in conversation