BookmarkSubscribeRSS Feed
Gladis6680
Obsidian | Level 7

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.

QuarterIce creamSalesConcession
Q1Chocolate$30$5
Q2Vanilla$25$15
Q3Butterscotch$50$10
Q4Strawberry$80$3
Q1Chocolate$30$2
Q2Vanilla$25$10
Q3Butterscotch$50$20
Q4Strawberry$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

QuarterIce creamSalesConcession%
Q1Chocolate$30$517%
Q2Vanilla$25$1560%
Q3Butterscotch$50$1020%
Q4Strawberry$80$34%
Q1Chocolate$30$27%
Q2Vanilla$25$1040%
Q3Butterscotch$50$2040%
Q4Strawberry$80$911%

I would appreciate your help.

thanks,

 

10 REPLIES 10
antonbcristina
SAS Super FREQ

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?  

Gladis6680
Obsidian | Level 7

Yes, % = concession/sales. I apologize I wasn't clear and it would be nice to have the % up to 2 decimal places.

Tom
Super User Tom
Super User

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.

Gladis6680
Obsidian | Level 7

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

Kathryn_SAS
SAS Employee

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

@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?

Gladis6680
Obsidian | Level 7
No I can do that.
Thanks,
Tom
Super User Tom
Super User

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;
Gladis6680
Obsidian | Level 7
Oh wow, I did not know that
thank you so much! I will try that
Ksharp
Super User

PROC REPORT would be the best PROC for your this question.

 

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
;

proc report data=have nowd;
column  Quarter  IceCream  Sales  Concession pct;
define Quarter/display;
define Sales/display;
define Concession/display;
define pct/computed format=percent8.2;
compute pct;
pct=Concession/Sales;
endcomp;
run;

Ksharp_0-1769846765209.png

 

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 10 replies
  • 190 views
  • 0 likes
  • 5 in conversation