Hi I had a quick question.
If I wanted to know the % between 2 numeric variables, which proc command would be the best to use.
For eg: below is a table.
| Quarter | Ice cream | Sales | Concession |
| Q1 | Chocolate | $30 | $5 |
| Q2 | Vanilla | $25 | $15 |
| Q3 | Butterscotch | $50 | $10 |
| Q4 | Strawberry | $80 | $3 |
| Q1 | Chocolate | $30 | $2 |
| Q2 | Vanilla | $25 | $10 |
| Q3 | Butterscotch | $50 | $20 |
| Q4 | Strawberry | $80 | $9 |
My var variables are Sales and Concession and my Class variable is Quarter. I thought of using the proc tabulate command, but I believe in a tabulate command the % is between a class variable and a numeric variable.
What I would like is the last column
| Quarter | Ice cream | Sales | Concession | % |
| Q1 | Chocolate | $30 | $5 | 17% |
| Q2 | Vanilla | $25 | $15 | 60% |
| Q3 | Butterscotch | $50 | $10 | 20% |
| Q4 | Strawberry | $80 | $3 | 4% |
| Q1 | Chocolate | $30 | $2 | 7% |
| Q2 | Vanilla | $25 | $10 | 40% |
| Q3 | Butterscotch | $50 | $20 | 40% |
| Q4 | Strawberry | $80 | $9 | 11% |
I would appreciate your help.
thanks,
Hi @Gladis6680, could you confirm that you would like to calculate % as concession/sales? Is it purposely rounded to the nearest integer value as well?
Yes, % = concession/sales. I apologize I wasn't clear and it would be nice to have the % up to 2 decimal places.
No need to call any PROCs. Just run a simple data step.
First let's convert your listing into a dataset so we have something the data step can use as input.
data have;
input Quarter :$2. IceCream :$12. Sales :comma. Concession :comma.;
format Sales Concession dollar6.2;
cards;
Q1 Chocolate $30 $5
Q2 Vanilla $25 $15
Q3 Butterscotch $50 $10
Q4 Strawberry $80 $3
Q1 Chocolate $30 $2
Q2 Vanilla $25 $10
Q3 Butterscotch $50 $20
Q4 Strawberry $80 $9
;
Now you just need to do simple division to calculate the percentage.
data want;
set have;
percent = Concession/Sales;
format percent percent8.2 ;
run;
NOTE: Remember to use valid names for your variables.
Hmm. Ok thanks I was trying not to include the %'s in the raw data. I guess I could create it in another data file
You could do this in PROC TABULATE with PCTSUM. I had to add a unique row variable since you are not grouping the quarter and icecream, but maybe the logic will work with your actual data.
data have;
input Quarter :$2. IceCream :$12. Sales :comma. Concession :comma.;
format Sales Concession dollar6.2;
cards;
Q1 Chocolate $30 $5
Q2 Vanilla $25 $15
Q3 Butterscotch $50 $10
Q4 Strawberry $80 $3
Q1 Chocolate $30 $2
Q2 Vanilla $25 $10
Q3 Butterscotch $50 $20
Q4 Strawberry $80 $9
;
run;
data have;
set have;
id=_n_;
run;
proc tabulate data=have;
class id;
class quarter icecream;
var sales concession;
table id*quarter*icecream, sales concession*(sum pctsum<sales>*f=8.);
run;
@Gladis6680 wrote:
Hmm. Ok thanks I was trying not to include the %'s in the raw data. I guess I could create it in another data file
Are you asking how to make a REPORT?
No need to make a physical copy of the data, just use a view to drive your report.
data for_report / view=for_report;
set have;
percent = Concession/Sales;
format percent percent8.2 ;
run;
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.