Help using Base SAS procedures

Totaling across groups and subgroups

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Totaling across groups and subgroups

Hello,

I need a quick help. I have the following data:

ID Group      Price

1      x            10

1      y            9

2      x           3

2      x           4

2      z            7

...

...

I wanted a subtotal of price by group and then by ID but would like to create a dataset out of parent data, without displaying using proc print (which took a long time).

Thanks.


Accepted Solutions
Solution
‎05-29-2015 01:47 PM
Super User
Posts: 10,490

Re: Totaling across groups and subgroups

proc summary data=have nway;

     class group id;

     var price;

     output out=want (drop=_type_ _freq_) sum=;

run;

If you want to know how many records were in each combination of group and id remove _freq_ in the code above. That is what that variable holds.

View solution in original post


All Replies
Solution
‎05-29-2015 01:47 PM
Super User
Posts: 10,490

Re: Totaling across groups and subgroups

proc summary data=have nway;

     class group id;

     var price;

     output out=want (drop=_type_ _freq_) sum=;

run;

If you want to know how many records were in each combination of group and id remove _freq_ in the code above. That is what that variable holds.

Occasional Contributor
Posts: 10

Re: Totaling across groups and subgroups

Thank you for the quick help. Is there anyway I can do the same thing using first. and last. processing?

Respected Advisor
Posts: 4,644

Re: Totaling across groups and subgroups

There is a simple way :

data have;

input ID Group $     Price;

datalines;

1      x            10

1      y            9

2      x           3

2      x           4

2      z            7

;

proc sort data=have; by group id; run;

data want;

do until(last.id);

    set have; by group id;

    priceSum = sum(priceSum, price);

    end;

keep group id  priceSum;

run;

PG

PG
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 240 views
  • 3 likes
  • 3 in conversation