BookmarkSubscribeRSS Feed
PETE
Calcite | Level 5

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

 

5 REPLIES 5
ballardw
Super User

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.

kthenaj
Fluorite | Level 6

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;

PETE
Calcite | Level 5

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?

dcruik
Lapis Lazuli | Level 10

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;
ballardw
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 903 views
  • 0 likes
  • 4 in conversation