mercredi 30 juin 2021

SAS adding an incremental counter based on a condition

I have the following dataset in SAS

Id   date        order   amount
101  5/20/2020     1       25
101  5/20/2020     2       25
101  5/20/2020     3        0
101  5/21/2020     1       25
101  5/21/2020     2       25

I need to add a counter only amount=25 based on ‘Id’, ‘Date’ and ‘Order’

Id   date        order   amount  Ctr
101  5/20/2020     1       25   1
101  5/20/2020     2       25   2
101  5/20/2020     3        0   0
101  5/21/2020     1       25   1
101  5/21/2020     2       25   2

Code:

Data want:
Set have;
By id date order;
Ctr+1;
If first.id and first.date and first.order) and amount=25 then ctr=1;
Run;

I am not getting the desired result. Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire