I have created a counter field in the past but now I want to create a counter field based on a condition within other variables in the data set. Here is my code:
data test_acct_2;
set test_acct_1;
by Proc_Date;
if Close_Date lt Proc_Date
then DIR = 1;
else DIR + 1; /*iterative statement*/
run;
In my dataset there are records for a particular field (Close_Date) that were created before the start of this particular process.
As a result, I have to recalculate the number of days for the DIS (DaysInStorage) field. Let me show the data set:
AccountNumber Proc_Date Close_Date Close_Amt Balance DaysInStorage
1001 6/10/19 5/24/18 $500 $500 512
1001 6/11/19 5/24/18 $500 $500 513
1001 6/12/19 5/24/18 $500 $500 514
I want to create this new counter field labeled DIS based on the difference between the close date and processing date.
In the event the close date came before the processing date for a particular account, I want the counter to start at 1, else
revert to the value of the DaysInStorage field.
Unfortunately, my code produces a 1 for every iteration.
Any assistance would be greatly appreciated. Thanks.
> Unfortunately, my code produces a 1 for every iteration.
You mean for every observation?
> In the event the close date came before the processing date for a particular account, I want the counter to start at 1
That's the case for every observation, so it makes sense that you always obtain 1.
Suggestion:
Show an example of the data where you have a close date > proc date within an account number and what you expect the result to look like.
If you have more than one account in your data then I suspect your result is very wrong because you should should be using BY AccountNumber Proc_date; (and the by proc date from the data you show is likely not needed as I don't see any processing using the By properties of Proc_date.
See if this gives you a start assuming your data is sorted by AccountNumber.
data test_acct_2; set test_acct_1; by AccountNumber; if first.AccountNumber then then DIR = 0; if close_date lt Proc_date then Dir+1; /* you don't say what really should happen when close_date ge Proc date in a way I understand */ run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.