Fluorite | Level 6

## YTD to Current Calculation

Hi All,

I have a data with multiple IDs with multiple dates. The example below contains one ID.  I have column C which is YTD, it has data inconsistencies. I would like to fix column C into column E and then calculate column F based on column E.

Column D is what i calculated.  I need help to arrive to the solution as column F.

Note: the code contain logic not to subtract from the first observation and from month one since it is YTD.

data want;

set have;

by id;

if first.id then rev_curr_want=rev_ytd_have;

else if month(date)=1 then rev_curr_want=rev_ytd_have;

else rev_curr_want=rev_ytd_have-lag(rev_ytd_have);

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: YTD to Current Calculation

Lag, and the companion function Dif, is a queued function. That means that when you use it in an IF/else block the result the Lag function returns is the last time the If/else was true.

I am not at all sure why a YTD involves subtraction but perhaps (not tested as too lazy to convert that table into a data step to test code):

```data want;
set have;
by id;
lryh = lag(rev_ytd_have);
if first.id then rev_curr_want=rev_ytd_have;
else if month(date)=1 then rev_curr_want=rev_ytd_have;
else rev_curr_want=rev_ytd_have - lryh;
run; ```
2 REPLIES 2
Super User

## Re: YTD to Current Calculation

Lag, and the companion function Dif, is a queued function. That means that when you use it in an IF/else block the result the Lag function returns is the last time the If/else was true.

I am not at all sure why a YTD involves subtraction but perhaps (not tested as too lazy to convert that table into a data step to test code):

```data want;
set have;
by id;
lryh = lag(rev_ytd_have);
if first.id then rev_curr_want=rev_ytd_have;
else if month(date)=1 then rev_curr_want=rev_ytd_have;
else rev_curr_want=rev_ytd_have - lryh;
run; ```
PROC Star

## Re: YTD to Current Calculation

Because, as @ballardw said, the LAG and DIF functions are queue managers, not lookbacks, putting them as a THEN assignment in an IF statement will not produce the "conditional lookback" that you want.

You have to update the LAG (or DIF below) with every observation, but use the DIF result only conditionally.  Embedding the LAG or DIF (or any function) as an argument of the IFN function will always do the update, even if that update will not be returned by the IFN.  (Same thing is true when using a LAG inside an IFC function that returns a character value).

As a result, this should work:

``````data want;
set have;
by id;
rev_curr_want=ifn(first.id=1 or month(date)=1,rev_ytd_have,dif(rev_ytd_have));
run;``````

Untested, in the absence of sample data in the form of a working SAS data step.

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

--------------------------
Discussion stats
• 2 replies
• 424 views
• 0 likes
• 3 in conversation