BookmarkSubscribeRSS Feed
HollyAB
Calcite | Level 5

I am trying to add the amount rows to get the total amount per ID, if the ID term and type match.

Here is what I have:

IDterm amounttype
18281112019FA-859110131
18281112019FA859110131
9999992019FA-3108170125
9999992019FA3108170125
88888882019SP-14550130133
88888882019SP14750130133

 

Here is what I would like

 

IDterm amounttype
18281112019FA0110131
9999992019FA0170125
88888882019SP200130133

 

 

1 REPLY 1
ballardw
Super User

Looks like a job for proc summary if you want a new data set:

For something like you show:

 

Proc summary data= have nway;
   class id term type;
   var amount;
   output out=work.want (drop=_type_ _freq_) sum=;
run;

The class statement tells the procedure which variables are used to define groups. Caution: if you have variable that has missing values that you need to treat as a "group" use the /missing option on the class statement.

Var statement tells SAS what variable you want to summarize.

The output statement says that you want to create output, names the data set and the SUM= will apply the Sum statistic to all variables on the Var statement and the name will be the same as the original variable. If you want multiple statistics for a variable you'll need to provide variable names or use the /autoname option to append the statistic name to the variables.

 

The odd looking NWAY on the Proc statement is one way of controlling the output so that only the full cross of the class variables is in the output. Without that option you would get additional summary lines in the data for 1) sum over all 2) sum of just the Id, term or type variables alone, 3) sum of the id & term, id & type and term & type as well as the sum will all three. The Drop option removes the variable _type_ which is an indicator of which of those combinations of variables were used for the particular output record in the data, and the _freq_ which is how many records were used for each output record.

 

Use Proc Print to display the results as desired.

 

You may not believe it now but this concept of doing multiple groups summaries is very powerful. There are a number of other options for creating specific groups as well. The procedure will in effect sort the data to create the groups if it is not sorted but there can be some issues with very large numbers of levels in multiple class variables.

 

If you want a report, these are things that people read, then perhaps:

Proc report data=have;
   column id term type amount;
   define id /group;
   define term/ group;
   define type/ group;
   define amount /sum;
run;

Proc report builds from left to right. So for Type to be used for grouping it would have to appear to the left of Amount in column order.

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
  • 1 reply
  • 3207 views
  • 1 like
  • 2 in conversation