BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
TheNovice
Quartz | Level 8

Hi all,

 

Not sure how to go about this at all...

 

I have data like below: DIFF = DUE - ACTV_AMT. 

 

ROWIDACTV_AMTDATEDUEDIFF
11235009Jul2020745.38395.38
21230010Aug2020778.7478.7
31230014Aug2020478.7178.7
412178.709Sep2020571.49392.79
51240011Sep2020392.79-7.21

 

I need to add another calculated column to get an output like:

The formula would be: Change =  Due(row2) - diff (row 1). 

Another complication: For the first row for Change, formula would be: Change = Due - 0. 

 

 

ROWIDACTV_AMTDATEDUEDIFFCHANGE
11235009Jul2020745.38395.38745.38
21230010Aug2020778.7478.7383.32
31230014Aug2020478.7178.70
412178.709Sep2020571.49392.79392.79
51240011Sep2020392.79-7.210

 

Ideally a proc sql solution is needed but I will take anything to complete this ad hoc first... appreciate any direction

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

I believe this untested data step would work.  I would test it if you could provide sample data in the form of a working data step.

 

The key element here is the lag function:

data want;
  set have;
  by id;
  change=ifn(first.id,due,due-lag(diff));
run;

I presume your data are sorted by id/date.  The program above embeds the lag function inside an IFN function.  The latter tests whether the record-in-hand is the start of an id - in which case the change variable is assigned a value equal to due.  Otherwise due is calculated as you describe.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

Ideally you won't even think of using sql for a task that requires observations to be processed in a defined order.

Similar problems are daily posted, so using the search function will show you at least n ways to solve the problem.

Have a look at the documentation of the lag-function.

mkeintz
PROC Star

I believe this untested data step would work.  I would test it if you could provide sample data in the form of a working data step.

 

The key element here is the lag function:

data want;
  set have;
  by id;
  change=ifn(first.id,due,due-lag(diff));
run;

I presume your data are sorted by id/date.  The program above embeds the lag function inside an IFN function.  The latter tests whether the record-in-hand is the start of an id - in which case the change variable is assigned a value equal to due.  Otherwise due is calculated as you describe.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
TheNovice
Quartz | Level 8
Thanks so much Mkeintz. Apologies for the delay but was pulled off for an urgent ask on a different project. I tested it and it works!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1315 views
  • 0 likes
  • 3 in conversation