Hello, i want to ask how can i sum each previous rows by variable SPCislo, here is sample of data:
| PK | DatumPodpisu | SPcislo | cislo |
| 501377324233203 | 23Sep2019 0:00:00,000 | 5100543 | 10 |
| 501377324233208 | 23Sep2019 0:00:00,000 | 5100543 | 10 |
| 501377324233213 | 23Sep2019 0:00:00,000 | 5100543 | 10 |
| 501377324274981 | 23Sep2019 0:00:00,000 | 5100543 | 10 |
| 501377324461518 | 10Mar2020 0:00:00,000 | 555 | 38,0352 |
| 501377324452148 | 11Mar2020 0:00:00,000 | 555 | 11,543 |
| 501377324458471 | 12Mar2020 0:00:00,000 | 555 | 195,7242 |
And i want to calculated sum of each row by rows group by SPCislo, like this:
| PK | DatumPodpisu | SPcislo | cislo | how to calculate this??? |
| 501377324233203 | 23Sep2019 0:00:00,000 | 5100543 | 10 | 10 |
| 501377324233208 | 23Sep2019 0:00:00,000 | 5100543 | 10 | 20 |
| 501377324233213 | 23Sep2019 0:00:00,000 | 5100543 | 10 | 30 |
| 501377324274981 | 23Sep2019 0:00:00,000 | 5100543 | 10 | 40 |
| 501377324461518 | 10Mar2020 0:00:00,000 | 555 | 38,0352 | 38,0352 |
| 501377324452148 | 11Mar2020 0:00:00,000 | 555 | 11,543 | 49,5782 |
| 501377324458471 | 12Mar2020 0:00:00,000 | 555 | 195,7242 | 245,3024 |
Thanks all for help 🙂
data want;
set have;
by SPcislo;
if first.SPcislo then c = 0;
c + cislo;
run;
data want;
set have;
by SPcislo;
if first.SPcislo then c = 0;
c + cislo;
run;
Or, very slightly optimized:
data want;
set have;
by SPcislo;
if first.SPcislo
then c = cislo;
else c + cislo;
run;
(at all FIRST. events, there's only one assignment instead of two)
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.