BookmarkSubscribeRSS Feed
Arch3nar
Calcite | Level 5

Hi Team,

 

I am trying to created a counter (as per the expected results below).
The data is sorted by Category Snapshot Focus_Date
The rule is that expected result (a counter), where the focus_date is present is the difference in months, This I can do with an INTNX function.
For the counter: When a new first.category arrives and the focus_date is blank I'd like the counter restarted at 0. If the next record is for the same category I'd like it to increase by 1.
If for the same category a new focus_date is found, I can use the intnx function. However, then I'd like to restart the counter at 0 if there is no focus_date in the next line.

If there have been no historic focus_date  items yet, then the result should also be 0..

 

This is the code I tried without incorporating the intnx:

 

data unique_months_2;
set unique_months;
by category snapshot focus_date;
if first.category or lag(focus_date) <> focus_date then expected_result = 0;
expected_result +1;
run;

 

My sample.

 

SnapshotCategoryFocus_DateExpected Result
31-Mar-22115-May-22-2
30-Apr-22115-May-22-1
31-May-221 0
30-Jun-221 1
31-Jul-22115-Jul-24-24
31-Jan-232 0
28-Feb-23215-Feb-23-1
31-Mar-232 0
30-Apr-232 1
31-Aug-213 0
30-Sep-213 0
31-Oct-213 0
31-Mar-21615-Mar-21-1
30-Apr-216 0
31-May-21620-May-21-1
30-Jun-216 0
31-Oct-227 0
30-Nov-227 0
31-Dec-227 0
31-Dec-231015-Jan-24-2
31-Jan-241015-Jan-24-1
29-Feb-2410 0
31-Mar-2410 1
30-Apr-241017-Apr-29-60
3 REPLIES 3
Patrick
Opal | Level 21

According to your rules why is the expected result -1 in below two rows in red?

Snapshot Category Focus_Date Expected Result
31-Jan-23 2   0
28-Feb-23 2 15-Feb-23 -1
31-Mar-23 2   0
... ...   ....
31-Mar-21 6 15-Mar-21 -1
30-Apr-21 6   0
Arch3nar
Calcite | Level 5

Apologies - there is a lot going on here. I am probably better taking a subset of the code to add a counter to, Even then I'm struggling. But to answer your question: The line following the red are 0. It counts backwards. However, I can update that with a INTNX formula. I'm not so concerned about these. It is more the increasing counts that I am concerned about.

Patrick
Opal | Level 21

@Arch3nar 

I or someone else can certainly help you with the code once we understand the rules.

That you provided sample data with the expected result is very helpful. My challenge right now is that I can't match the rules you formulated with the sample data you provided and I also couldn't figure out what the rules might be just based on the sample data. That's why I'm asking clarification questions. Well... may-be someone else will understand.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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