Hello
One simple question.
I want to calculate the sum of a variable in my long formatet dataset.
My dataset looks like this:
ID gmfcs_dummy
1000 0
1000 1
1000 1
2000 0
2000 0
2000 0
3000 1
3000 1
3000 1
I want to calculate the sum of gmfcs_dummy so it sums up in rising order like this:
ID gmfcs_dummy gmfcs_dummy2
1000 1 1
1000 1 2
2000 0 0
2000 0 0
2000 0 0
3000 1 1
3000 1 2
3000 1 3
How can I do this?
Kind regards Frank
Hi @frakje
You can use this:
data have;
input ID gmfcs_dummy;
datalines;
1000 0
1000 1
1000 1
1000 0
2000 0
2000 0
2000 0
3000 1
3000 1
3000 1
;
run;
data want;
set have;
by ID;
if first.ID then gmfcs_dummy2 = 0;
if gmfcs_dummy=1 then gmfcs_dummy2 + gmfcs_dummy;
else gmfcs_dummy2 = 0;
run;
Hi @frakje
You can use this:
data have;
input ID gmfcs_dummy;
datalines;
1000 0
1000 1
1000 1
1000 0
2000 0
2000 0
2000 0
3000 1
3000 1
3000 1
;
run;
data want;
set have;
by ID;
if first.ID then gmfcs_dummy2 = 0;
if gmfcs_dummy=1 then gmfcs_dummy2 + gmfcs_dummy;
else gmfcs_dummy2 = 0;
run;
@frakje wrote:
Hello
One simple question.
I want to calculate the sum of a variable in my long formatet dataset.
My dataset looks like this:
ID gmfcs_dummy
1000 0
1000 1
1000 1
2000 0
2000 0
2000 0
3000 1
3000 1
3000 1
I want to calculate the sum of gmfcs_dummy so it count upwise like this:
ID gmfcs_dummy gmfcs_dummy2
1000 1 1
1000 1 2
2000 0 0
2000 0 0
2000 0 0
3000 1 1
3000 1 2
3000 1 3
How can I do this?
Kind regards Frank
Why does your intended result contain only 2 observations for ID 1000, but 3 for ID 2000?
Hallo Kurt
It's because the observations/patients has an new treatment every year. Each patient can have different entry dates in the follow-up program.
Patient with ID 1000 have e.g. first treatment in 2016, second treatment in 2017 and third treatment in 2018. Therefore there is three records for that specific patient.
But 1000 has only 2 (TWO) observations in your result.
Oh, sorry!
Typing error. It should have been three
Then try this:
data want;
set have;
by id;
if first.id
then gmfcs_dummy2 = gmfcs_dummy;
else gmfcs_dummy2 + gmfcs_dummy;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.