SAS Procedures

Help using Base SAS procedures
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sweetgorgor
Calcite | Level 5

Hi, first let me apologize for not being more clear on the title as I am not sure how to discribe what I am looking for in just a few words. I am essentially trying to derive the Extended Penalty column in the table below:

DateMistakes_FlgNumber Of consecutive mistakesPenaltyExtended_Penalty
1/1/20130000
1/2/201311100
1/3/201312100
1/4/20130000
1/5/201311100
1/6/201312100
1/7/201313100
1/8/201314100
1/9/201300010
1/10/201300010
1/11/201300010
1/12/201300010
1/13/20130000

where if the person makes mistakes for 3 or more consecutive days, he gets punished by paying an daily extended penalty of the previous penalty equal to the number of consecutive days he made a mistake. In my example, the first time he made a mistake for 2 consecutive days, no extended penalty has incurred since it's less than the 3 days criteria. However, he then made a mistake for 4 consecutive days, therefore he will have to pay the extended penalty for 4 more days after he stop making mistakes. Can someone help I tried everything and I can't get it work!

Thanks a bunch in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

What do you have working? I'm assuming you have an ID field somewhere as well?

If someone makes a mistake for 3 days then there's a 3 day penalty or is more than 3?

What happens if they make mistakes for 8 days, stop for 4 days and then starts again?

View solution in original post

5 REPLIES 5
Reeza
Super User

What do you have working? I'm assuming you have an ID field somewhere as well?

If someone makes a mistake for 3 days then there's a 3 day penalty or is more than 3?

What happens if they make mistakes for 8 days, stop for 4 days and then starts again?

SteveDenham
Jade | Level 19

Or extending Reeza's question:

-Mistakes for 8 days, followed by no mistakes 4 days --> Extended penalty on the 4 no mistake days

-This is then followed by: Mistakes for 3 days after no mistakes for 4 days, followed by no mistakes for 4 days --> Extended penalty on the 3 mistake days.  I'm clear to this point.  Now what happens on the first no mistake day in this section--is it double extended penalty (1 day carried over from the 8 day period, plus the first from the more recent 3 day period), or just extend from the recent 3 day and have a single extended penalty?

This is a lot like coding for disease cases.  If we say that no new case can begin until after n days have passed since the last day of signs or symptoms the logic is similar.  It is just that in the epidemiology situation n is usually predefined, while here it is a matter of looking backward.

Steve Denham

sweetgorgor
Calcite | Level 5

you guys are so awesome! I was comtemplating the same issues while looking into the contracts (People who wrote those contracts never thought of those issues!) I will try to simply the problem first, if i still have some questions in regards to the coding aspect I will follow it up on this thread. Thanks!

snoopy369
Barite | Level 11

To answer the 'simple' case, I think the easiest method to explain is simply to have one more column: "Number of remaining extended penalty days".  Make it work exactly like your "Number of consecutive mistakes" column, but in reverse - count down from 4 to 3 to 2 to 1.

Aad
Calcite | Level 5 Aad
Calcite | Level 5

The excellent explanation of your problem challenged me to write a 9-statement solution plus comments as follows. Hope you enjoy it ...

    Data Mistakes;

       /*-----------------------------------------------------------------*/

       /* Generate simulation table with dates and some random Mistakes   */

       /*-----------------------------------------------------------------*/

           Attrib Date    Length = 4 Format = Date11.

                  Mistake Length = 2 Format =      1.;

           Do Date    = '01-jan-2013'd to '31-dec-2013'd;

              Mistake =  Round (RanUni (5), 1);

              Output;

              End;

           Run;

/*1*/Data Mistakes;

       /*-----------------------------------------------------------------*/

       /* Re-generate table, including PENALTIES_TO_GO and PENALTY values */

       /*-----------------------------------------------------------------*/

/*2*/     Attrib Date            Length = 4 Format = Date9.

                 Mistake         Length = 3 Format =     1.

                 Penalty         Length = 3 Format =     1.

                 Penalties_to_go Length = 3 Format =     8.

/*3*/     Set    Mistakes;

       /*-----------------------------------------------------------------*/

       /* PENALTY becomes 1 if current and last 3 dates all had MISTAKE=1 */

       /*-----------------------------------------------------------------*/

/*4*/     Penalty = Mistake  * Lag1 (Mistake)

            * Lag2 (Mistake) * Lag3 (Mistake);

       /*-----------------------------------------------------------------*/

       /* Accumulate the number of PENALTIES_TO_GO                        */

       /*-----------------------------------------------------------------*/

/*5*/     Penalties_to_go + Penalty;

       /*-----------------------------------------------------------------*/

       /* If current+last 3 days had MISTAKE=1, and 4 days ago MISTAKE=0, */

       /* then increase the PENALTIES_TO_GO by an additional 3 penalties. */

       /*-----------------------------------------------------------------*/

/*6*/     If Penalty > Lag4 (Mistake) >= 0 Then Penalties_to_go + 3;

       /*-----------------------------------------------------------------*/

       /* Assign a PENALTY if current date has MISTAKE=0 and there are    */

       /* still one or more PENALTIES_TO_GO.                              */

       /*-----------------------------------------------------------------*/

/*7*/     Penalty = (Error = 0 and Penalties_to_go > 0);

       /*-----------------------------------------------------------------*/

       /* Decrease the number of PENALTIES_TO_GO only if PENALTY=1        */

       /*-----------------------------------------------------------------*/

/*8*/     Penalties_to_go + -Penalty;

/*9*/     Run;

     Proc Print Uniform;

           Run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 1540 views
  • 7 likes
  • 5 in conversation