DATA Step, Macro, Functions and more

How to take a SAS Dataset and sum up individual columns

Reply
New Contributor
Posts: 4

How to take a SAS Dataset and sum up individual columns

Hi - getting bogged down on the simple idea of taking mutiple columns from a dataset and summing up certain columns. To give you an example of what i'm doing, i want to take the below example and sum up the "eligible column" so that the final column is summed up. Any suggestions on how to get moving on it?

 

StyleTypeRegionEligible
LCLongAsia1
LCLongAsia1
LCLongAsia0
LCLongAsia1
SCShortEmea0
SCShortEmea1
    
    
    
LCLongAsia3
SCShortEmea1

 

Super User
Posts: 11,343

Re: How to take a SAS Dataset and sum up individual columns

If you want a dataset for output them Proc means or summary:

 

proc summary data=have nway;

   class style type region;

   var eligible;

   output out=want sum=;

run;

 

The sum= says keep the same variable but sum within the combinations of style, type and region.

There will be to other variables in the output, _type_ that can be used to determine combinations of the class variable when not using the NWAY option and _freq_ that says how many records were used.

Occasional Contributor
Posts: 13

Re: How to take a SAS Dataset and sum up individual columns

Do you want another variable in your data set of the sum? Do you just want to know the summed value? Wasn't exactly sure what you wanted so here are two options. Hope they help.

 

data Pete;
input Style $ Type $ Region $ Eligible;

 

/*Option 1: Creating a cumulative sum variable*/
retain EligibleSum 0;
EligibleSum = EligibleSum + Eligible;

 

datalines;
LC Long Asia 1
LC Long Asia 1
LC Long Asia 0
LC Long Asia 1
SC Short Emea 0
SC Short Emea 1
LC Long Asia 3
SC Short Emea 1
;


/*Option 2: Simply determining the sum of the Eligible variable*/
proc print data = Pete;
sum Eligible;
run;

New Contributor
Posts: 4

Re: How to take a SAS Dataset and sum up individual columns

Thank you for the guidance.

 

One follow up question - whats the best way to "ignore" a column in a dataset that you don't need? If=Delete function?

Frequent Contributor
Posts: 130

Re: How to take a SAS Dataset and sum up individual columns

[ Edited ]

You can use a DROP statement, either in parenthesis during the DATA statement, or after the SET statement, not in parenthesis.

 

data want (DROP=variable);
set have;
run;

data want;
set have;
DROP variable;
run;
Super User
Posts: 11,343

Re: How to take a SAS Dataset and sum up individual columns

If you use the proc summary approach any variable not explicitly listed in the syntax will not be in the output.

Ask a Question
Discussion stats
  • 5 replies
  • 245 views
  • 0 likes
  • 4 in conversation