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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

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;

 

 

View solution in original post

6 REPLIES 6
ed_sas_member
Meteorite | Level 14

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;

 

 

Kurt_Bremser
Super User

@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?

frakje
Calcite | Level 5

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.

frakje
Calcite | Level 5

Oh, sorry!

 

Typing error. It should have been three

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 6 replies
  • 1054 views
  • 0 likes
  • 3 in conversation