BookmarkSubscribeRSS Feed
sarymay
Calcite | Level 5

I am having a really hard time trying to figure out how to create a table with the columns equal to all values for one variable, the rows equal to all possible values for another variable, and the entries equal to the sum of all values for which the two values intercept. Can anyone help me with this?

EX:

color     letter     cost

blue         A          2

red          F          4

orange     C          1    

yellow     K          3

orange     C          9

yellow     K          1

blue        F          7

yellow     A          2

Result:


AFCK
Blue27
Red4
Orange10
Yellow24
2 REPLIES 2
ballardw
Super User

What do you want if there are two values of Blue A in the data? Should the Cost be added, averaged or something else?

Assuming your data is in a SAS data set and varaible names are color, letter and cost this might work if you want the sum of costs:

proc tabulate data=yourdatasetname ;

     Class color letter;

     var cost;

     table color='',

             letter=''*sum=''*cost=''*f=f6.0

             / misstext=' ';

run;

The ='' parts above, which are two single quotes but hard to read here, suppress the variable name and statistic label from appearing the output. Warning for PROC TABULATE new users, if color or letter are missing the record will be excluded from the output. The *f=f6.0 is do display the sum of cost as an integer up to 6 digits wide. The misstext option says to display a blank for missing sums of cost.

If you want a dataset then there are things to consider about what you are doing with cost.

Reeza
Super User

Assuming you want cost summed as in the example...

There's many ways to get the second step, but proc tabulate also shows it in that format.

I think proc summary could do it in one step, but not sure.

data have;

input colour   $  letter  $   cost;

cards;

blue      A          2

red       F          4

orange    C          1   

yellow    K          3

orange    C          9

yellow    K          1

blue      F          7

yellow    A          2

;

ods table table=summary;

proc tabulate data=have;

    class colour letter;

    var cost;

    table colour, letter*cost*sum;

run;

proc transpose data=summary out=summary2(drop=_name_);

    by colour;

    id letter;

    var cost_sum;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 385 views
  • 0 likes
  • 3 in conversation