BookmarkSubscribeRSS Feed
scb
Obsidian | Level 7 scb
Obsidian | Level 7

I have the below dataset, and both dataset is linked by ID.  I would like to deduct the penalty from the winning; and to get the final penalty balance for individual case.  Anyone can help? Many Thanks.

 

 

DATA PENALTY;
INPUT GAME $ CASE $ ID $ PENALTY;
CARDS;
CC C1 111 480
HL H1 111 1120
SI S1 111 800
CC C2 112 750
SI S2 113 829
;
RUN;

 

DATA WINNING;
INPUT ID $ WINNING;
CARDS;
111 3000
112 700
113 800
;
RUN;

 

Desired output:

 

Dataset Winning (Penalties variable is the aggregate of penalty variable for individual ID, and must not more than the "winning")

 

ID     Winning  Penalties  Final

111   3000       2400        600

112   700         700          0

113   800         800          0

 

Dataset Penalty (Balance variable is the balance penalties that have not been deducted from the winning)

 

GAME   CASE  ID      PENALTY   BALANCE
CC        C1       111   480             0
HL         H1       111   1120           0
SI          S1       111    800            0
CC        C2       112   750            50
SI          S2       113   829            29

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

How come final=0 for ID=113?

scb
Obsidian | Level 7 scb
Obsidian | Level 7

The penalties is 829, but winning is 800 only.  Penalties cannot more than winning, hence can only deduct max 800 for ID 113.

PeterClemmensen
Tourmaline | Level 20

One way

 

data want;
    format id winning Penalties Final;
    if _N_=1 then do;
        dcl hash h(ordered:'y');
        h.definekey('ID');
        h.definedata('Penalties');
        h.definedone();

        do until (lr);
            set penalty end=lr;
            if h.find() ne 0 then Penalties=penalty;
            else Penalties+penalty;
            h.replace();
        end;
    end;

    set winning;
    rc=h.find();

    if winning <= Penalties then Penalties=winning;
    Final=winning-Penalties;

    keep id winning Penalties Final;
run;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 764 views
  • 0 likes
  • 2 in conversation