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

I have a data series as follows. I'd like to come up with the sum for column A for each continuous series of C for each ID. For instance, the first continuous series in C for ID 101, the sum in A is 5; in the next continuous series in C for ID 101, the sum in A is 4. I cannot use proc sql to get the sum because I cannot group by ID and C as the number in C is not unique.My intuition is that I probably need some retain statement along with a loop to accomplish this. But I'm not sure how to make it work.  I'd appreciate any help in coding it. Thanks!

 

IDABC
101106
101116
101116
101016
101106
101116
101000
101116
101106
101016
101016
101106
101106
102015
102115
102115
102015
102105
1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

All you might need is to replace

 

output;

 

with

 

if C ne 0 then output;

 

in @ChrisNZ's code above.

 

Otherwise, it does exactly what you want.

PG

View solution in original post

10 REPLIES 10
ChrisNZ
Tourmaline | Level 20

Like this?

data WANT; 
  set HAVE;
  by ID C notsorted;
  SUMA+A;
  if last.C then do;
    output;
    SUMA=0;
  end
run;

 

Tami99
Calcite | Level 5

Thank you for your quick reply, Chris. But I have a question. In your code, the last.c refers to the last observation in column C for an ID (obs #13), or the last observation in Column C in a continuous series where C is not zero (obs #6).  I'd like to accumulate A by the latter. Thanks!

 

obsIDABC
1101106
2101116
3101116
4101016
5101106
6101116
7101000
8101116
9101106
10101016
11101016
12101106
13101106
14102015
15102115
16102115
17102015
18102105
PGStats
Opal | Level 21

All you might need is to replace

 

output;

 

with

 

if C ne 0 then output;

 

in @ChrisNZ's code above.

 

Otherwise, it does exactly what you want.

PG
ChrisNZ
Tourmaline | Level 20

Unsure I understand. Please provide the desired utput.

Maybe don't read the observations where C equals 0?

 

where C;

 

Tami99
Calcite | Level 5

The change that PGStats suggested works! Thank you both!!!

Tami99
Calcite | Level 5

I have a follow up programming question. With the codes above, I did get the output for each cumulative total of column A in a series. However, is there any way to populate this total to each continuous series. The desired output looks like the following:

 

obsIDABCSUMA
11011065
21011165
31011165
41010165
51011065
61011165
71010000
81011164
91011064
101010164
111010164
121011064
131011064
141020153
151021153
161021153
171020153
181021053

 

PGStats
Opal | Level 21

Sure.

 

data WANT; 

suma = 0;

do until(last.c);
	set have;  by id c notsorted;
	suma = suma + a;
	end;

do until(last.c);
	set have;  by id c notsorted;
	output;
	end;

run;

(untested)

PG
Tami99
Calcite | Level 5

Thank you so much for the quick reply. I tested the codes and got the following results.

obsIDABCSUMA
1101106.
2101116.
3101116.
4101016.
5101106.
61011165
71010000
8101116.
9101106.
10101016.
11101016.
12101106.
131011064
14102015.
15102115.
16102115.
17102015.
181021053

 

Is there a way to fill those missing values with the accumulated total as below? Thank you!!!

 

obsIDABCSUMA
11011065
21011165
31011165
41010165
51011065
61011165
71010000
81011164
91011064
101010164
111010164
121011064
131011064
141020153
151021153
161021153
171020153
181021053
PGStats
Opal | Level 21

Looks like your have dataset already contained variable suma. Try this then:

 

data WANT; 

suma = 0;

do until(last.c);
	set have(drop=suma);  by id c notsorted;
	suma = suma + a;
	end;

do until(last.c);
	set have(drop=suma);  by id c notsorted;
	output;
	end;

run;
PG
Tami99
Calcite | Level 5

Yep!!! Now it works wonders!!! Thank YOU!!!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 721 views
  • 1 like
  • 3 in conversation