BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
acrosb
Calcite | Level 5

Hi, 

 

I have data that looks like this:

yyyymm    channel               revenue

202001     standard              $150

202001     non-standard       $200

202001     hybrid                  $50

202002     standard              $100

202002     non-standard      $100

202002     hybrid                  $100

 

I want to report the percent of revenue coming from each channel in each month. Here is the output I'm looking for:

                          202001          202002

standard            37.5%             33.3%

non-standard     50%             33.3%

hybrid                12.5%             33.3%

 

The below sql code give me the correct numbers (the percent), but the format of the table/output is not want I want. I attempted to do the procedure in proc freq and proc tabulate, but I can't seem to get those to work. The freq code below takes a really long time to run, so I actually don't know if it does give the correct results. Can anyone help with this?

proc sql;
create table temp2 as
select yyyymm, channel, revenue, revenue/sum(revenue) as pctPop format=percent8.1
from (select yyyymm, channel, sum(revenue) as revenue from temp1 group by yyyymm, channel)
group by yyyymm;
select * from temp2;
quit;

proc freq data=temp1;
  tables revenue*(yyyymm channel);
run;

proc tabulate data=temp1;
class channel yyyymm;
var revenue;
table channel, pctsum*niw*yyyymm;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
data have;
informat yyyymm yymmn6. channel $15. revenue dollar.;
format yyyymm yymmn6. revenue dollar8.;
input yyyymm    channel           revenue ;
cards;
202001     standard              $150
202001     non-standard       $200
202001     hybrid                  $50
202002     standard              $100
202002     non-standard      $100
202002     hybrid                  $100
;;;;
run;

proc tabulate data=have;
class channel yyyymm;
table channel, yyyymm*(colpctn);
freq revenue;
run;

This should get you closer using just PROC TABULATE if your dollars are all whole units. Not sure how it works if they're decimals.

View solution in original post

7 REPLIES 7
Reeza
Super User
data have;
informat yyyymm yymmn6. channel $15. revenue dollar.;
format yyyymm yymmn6. revenue dollar8.;
input yyyymm    channel           revenue ;
cards;
202001     standard              $150
202001     non-standard       $200
202001     hybrid                  $50
202002     standard              $100
202002     non-standard      $100
202002     hybrid                  $100
;;;;
run;

proc tabulate data=have;
class channel yyyymm;
table channel, yyyymm*(colpctn);
freq revenue;
run;

This should get you closer using just PROC TABULATE if your dollars are all whole units. Not sure how it works if they're decimals.

PaigeMiller
Diamond | Level 26

For percents, you can use PROC FREQ

 

proc freq data=have;
    by yyyymm;
    table channel;
    weight revenue;
run;

 

or

 

proc freq data=have;
    table channel*yyyymm/norow nopercent;
    weight revenue;
run;

 

 

 

@acrosb Please notice how @Reeza has modified your data to be SAS data step code. That is how you should present data from now on.

--
Paige Miller
acrosb
Calcite | Level 5

Thank you! Yes, I will present my data in a data step from now on.

acrosb
Calcite | Level 5

Thank you for your help! This is perfect.

acrosb
Calcite | Level 5

When I output the Tabulate results (using "out=datasetname" in the proc tabulate statement), the columns/rows flip. Is there any (simple) way to keep the format of the table when putting it into a dataset? I was the years/months to be columns in my outputted dataset.

Reeza
Super User

Unfortunately not, but you can use PROC TRANSPOSE to flip it back.

 

proc transpose data=long out=want;
by channel;
id yyyymm;
var pctn_01;
run;
acrosb
Calcite | Level 5

Ah, ok. Thanks again!

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