Fluorite | Level 6

## How to calculate percentage of each group in SAS?

Hi all,

I'd like to ask u guys some questions related to SAS statistical method. (SQL, any code,,)

What I want to see is the percentage of food sales on a daily basis based on the data attached.

Please refer to information below to understand the attached file. I want to compare these three groups.

[Excel Sheet1]

1) Number of foods sold for same period with the intervention date of last year (Nov,19th 2018 ~ Dec, 14th 2018 / 4 weeks exclude Sat & Sun) - No intervention

2) Number of food sales before the intervention (Oct, 25th 2019 ~ Nov, 15th 2019 / 4 weeks exclude Sat & Sun) - No intervention

3) Number of food sales during the intervention (Nov,18th 2019 ~ Dec, 13th 2019 / 4 weeks exclude Sat & Sun) - Intervention

[Background]
I classified all foods in school cafeteria as nutrition score and put Alphabet score stickers on them during the intervention time. (A,B,C,D,E - A has the best nutrition composition). And I want to know how these nutri-score stickers affect people's consumption habit. I want to do interrupted time series analysis ultimately, but I want to see the ratio of each group before that.

It will be much appreciated if u guys let me know how to solve this problem. This is for my thesis.

Please feel free to comment on me if u have questions.

1,2) Caculation system Nutri-Score

3) my key thesis' graph in harvard Univ.

7 REPLIES 7
Diamond | Level 26

## Re: How to calculate percentage of each group in SAS?

Most of us will not download Excel files (or other Microsoft Office files) because they are a security risk.

Please provide a portion of your data as a SAS DATA step.

PROC FREQ calculates percentages of each group.

--
Paige Miller
Fluorite | Level 6

## Re: How to calculate percentage of each group in SAS?

proc sql;
create table posco1 as
select *, sum(ea) as sum_ea
from posco
;

Diamond | Level 26

## Re: How to calculate percentage of each group in SAS?

I cannot work with data in a screen capture. The specific request was "Please provide a portion of your data as a SAS DATA step." Here are Instructions: How to create a data step version of your data AKA generate sample data for forums

What is the EXACT calculation you want to do with this data? Your PROC SQL code does not compute percents, what is the numerator and what is the denominator of the percent that you want?

--
Paige Miller
Fluorite | Level 6

## Re: How to calculate percentage of each group in SAS?

``````proc sql;
create table posco1 as
select *, sum(ea) as sum_ea
from posco
;

proc sort data = posco1 out = posco2 nodupkey;
run;

proc sql;
create table posco2 as
select *, sum_ea / sum(sum_ea) * 100 as percent
from posco1
group by day
quit;``````

I made this mistake because I wasn't used to it.
I'm so sorry to bother you.

Diamond | Level 26

## Re: How to calculate percentage of each group in SAS?

@chiyoung wrote:
``````proc sql;
create table posco1 as
select *, sum(ea) as sum_ea
from posco
;

proc sort data = posco1 out = posco2 nodupkey;
run;

proc sql;
create table posco2 as
select *, sum_ea / sum(sum_ea) * 100 as percent
from posco1
group by day
quit;``````

`Also, I am calculating each ratio in this way.`

It seems as if you have answered your own question. Or am I misunderstanding something?

--
Paige Miller
Fluorite | Level 6

## Re: How to calculate percentage of each group in SAS?

``````data WORK.POSCO;
infile datalines dsd truncover;
format day DATE9.;
datalines;
19NOV2018 a 5
19NOV2018 a 38
19NOV2018 a 4
19NOV2018 a 10
19NOV2018 a 6
19NOV2018 a 30
19NOV2018 a 0
19NOV2018 a 5
;``````

I made this mistake because I wasn't used to it.
I'm so sorry to bother you.

Super User

## Re: How to calculate percentage of each group in SAS?

Given what you've posted, consider PROC FREQ.

It calculates the percentages automatically. So what you need to figure out is how to filter your data set but the percentage part is trivial. If you cannot provide real data that's understandable, instead generalize your problem and consider using datasets from SASHELP library, which are available to all SAS users instead.

``````proc freq data=sashelp.cars;
table origin;
run;``````

@chiyoung wrote:

Hi all,

I'd like to ask u guys some questions related to SAS statistical method. (SQL, any code,,)

What I want to see is the percentage of food sales on a daily basis based on the data attached.

Please refer to information below to understand the attached file. I want to compare these three groups.

[Excel Sheet1]

1) Number of foods sold for same period with the intervention date of last year (Nov,19th 2018 ~ Dec, 14th 2018 / 4 weeks exclude Sat & Sun) - No intervention

2) Number of food sales before the intervention (Oct, 25th 2019 ~ Nov, 15th 2019 / 4 weeks exclude Sat & Sun) - No intervention

3) Number of food sales during the intervention (Nov,18th 2019 ~ Dec, 13th 2019 / 4 weeks exclude Sat & Sun) - Intervention

[Background]
I classified all foods in school cafeteria as nutrition score and put Alphabet score stickers on them during the intervention time. (A,B,C,D,E - A has the best nutrition composition). And I want to know how these nutri-score stickers affect people's consumption habit. I want to do interrupted time series analysis ultimately, but I want to see the ratio of each group before that.

It will be much appreciated if u guys let me know how to solve this problem. This is for my thesis.

Please feel free to comment on me if u have questions.

1,2) Caculation system Nutri-Score

3) my key thesis' graph in harvard Univ.

Discussion stats
• 7 replies
• 2195 views
• 2 likes
• 3 in conversation