BookmarkSubscribeRSS Feed
Cruiser13
Fluorite | Level 6

I am trying to create and ALL entry for a variable by summing the Counts of the other entries by different variables in SAS. Here is an example that might help you better understand what I am asking:

 

Example dataset:

 

PetsexcolorCount
dogFB2
dogMB5
dogFW4
dogMW1
dogFY7
dogMY3
catFB5
catMB2
catFW2
catMW3
catFY6
catMY2
birdFB1
birdMB1
birdFW3
birdMW2
birdFY2
birdMY4

 

I want to use SAS to create a Pet called 'ALL' that sums the Count by sex and color.

For this example, it would look like this:

 

ALLFB8
ALLMB8
ALLFW9
ALLMW6
ALLFY15
ALLMY9

 

How would I use SAS to create the Pet = 'ALL' part?

 

Thanks in Advance!

3 REPLIES 3
Reeza
Super User
Look into PROC MEANS and specifically the TYPES/WAYS statements. But unless you want the actual word "all" this will get you want you want, and you can add ALL after. If you need it at multiple levels, ie ALL, plus by individual pet, then PROC TABULATE or PROC MEANS can also do that but I may be overcomplicating what you'd want.

proc means data=have;
class sex color;
ways sex*color;
output out=want N=;
run;

proc freq data=have;
table sex*color / list;
run;
PaigeMiller
Diamond | Level 26
proc summary data=have;
    class pet sex color;
    types sex*color;
    var count;
    output out=_sums_ sum=;
run;

The rows of the output table have missing values for the variable named PET. If you really have to have PET="ALL", a simple data step after PROC SUMMARY will get you there (or alternatively a custom format will do that if that works better for you).

--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 531 views
  • 1 like
  • 3 in conversation