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

Hi,

 

I have >5.000 rows and 130 columns.

Column 1: Postal codes (inkl.duplicates)

Column 2-129 with €-values (names of columns: sp1-sp129)

 

What I need is a new row with the result of the sum of all rows for each numeric column (sp1-sp129), if the postal code is XXXXX. How can I do this with a short sql-code? Thank you for help!

 

For example:

postal_code sp1 sp2 spN
11111 65 78 ...
11111 12 65 ...
22222 87 21 ...
33333 42 45 ...
22222 65 87 ...
11111 24 86 ...
1111_sum 101 229 ...
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

I wouldn't even attempt this in PROC SQL

 

proc summary data=have nway;
    class postal_code;
    var sp1-sp129;
    output out=_sums_ sum=;
run;

proc sort data=have;
    by postal_code;
run;

data want;
    length postal_code $ 10;
    set have _sums_;
    by postal_code;
    if _type_=1 then postal_code=cats(postal_code,'_sum');
run;
--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

I wouldn't even attempt this in PROC SQL

 

proc summary data=have nway;
    class postal_code;
    var sp1-sp129;
    output out=_sums_ sum=;
run;

proc sort data=have;
    by postal_code;
run;

data want;
    length postal_code $ 10;
    set have _sums_;
    by postal_code;
    if _type_=1 then postal_code=cats(postal_code,'_sum');
run;
--
Paige Miller
Konkordanz
Pyrite | Level 9
Thank for response, is working...but: actually I dont want to sum all postal-codes But only certain codes; in my example the 11111. So I need a way to summarize the certain data with a filter and add the result as a new row into my dataset. Do you have a short solution? thanks!
Konkordanz
Pyrite | Level 9

double-post: I got it! I just put the Where-command into the summary-command, built a table per sum-group and combined all tables afterwards.

Thanks!

Reeza
Super User

How do you include duplicates with the different values but then sum them up if the postal code is XXX? Those are contradictory requirements.

Do you mean you only want postal codes with the same characters added up, ie with the pattern of letters and just the total at the bottom? Do you expect to see a line of 22222_sum as well?

 


@Konkordanz wrote:

Hi,

 

I have >5.000 rows and 130 columns.

Column 1: Postal codes (inkl.duplicates)

Column 2-129 with €-values (names of columns: sp1-sp129)

 

What I need is a new row with the result of the sum of all rows for each numeric column (sp1-sp129), if the postal code is XXXXX. How can I do this with a short sql-code? Thank you for help!

 

For example:

postal_code sp1 sp2 spN
11111 65 78 ...
11111 12 65 ...
22222 87 21 ...
33333 42 45 ...
22222 65 87 ...
11111 24 86 ...
1111_sum 101 229 ...

 

Konkordanz
Pyrite | Level 9
Thank for response and sry for inaccurate description!
Well, I actually dont want to sum all postal-codes. I mean...it would be okay, afterwards I would just filter away the lines i doesnt need. But actually I want to sum certain codes; in my example the 11111. So I need a way to summarize the certain data with a filter and add the result as a new row into my dataset. Do you have a short solution? thanks!
Patrick
Opal | Level 21

It's often useful to show the desired result based on the sample data (ideally provided via a SAS data step). Below 3 want options for what you might ask for. Check if any of the options fits your requirement or else let us know what comes closest and what's still missing.

As for a total line: That's then more about creating a report than a new table. So if you need a report then please tell us - and SHOW us how this report would need to look like.

data have;
  input postal_code $ sp1 sp2;
datalines;
11111 65 78
11111 12 65
22222 87 21
33333 42 45
22222 65 87
11111 24 86
;

data want_1;
  set have;
  spN=sum(of sp:);
run;
data want_2;
  set have;
  if postal_code='11111' then
    spN=sum(of sp:);
run;
data want_3;
  set have;
  if postal_code='11111';
  spN=sum(of sp:);
run;

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
  • 6 replies
  • 2445 views
  • 6 likes
  • 4 in conversation