BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
atnk
Fluorite | Level 6

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; 

 

ABCDEF
dateidrev_ytd_haverev_curr_calcrev_ytd_wantrev_curr_want
201911adf1010101010
201912adf10100100
202001adf101331313
202002adf10130130
202003adf10130130
202004adf10130130
202005adf10 -13130
202006adf10 0130
202007adf101414141
202008adf10140140
202009adf10140140
202010adf10140140
202011adf10140140
202012adf10140140
202101adf101621616
202102adf10160160
202103adf10 -1600
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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; 

View solution in original post

2 REPLIES 2
ballardw
Super User

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; 
mkeintz
PROC Star

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

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 2 replies
  • 424 views
  • 0 likes
  • 3 in conversation