## How to Calculate Percentage with a PROC SQL

Solved
Super Contributor
Posts: 413

# How to Calculate Percentage with a PROC SQL

Hello everyone, I think I have a very simple question but I can't handle it. I'm trying to get percentage of values of the variables. I think I'm so close the desired output but I want to be sure. Could you help me, please ?

Sample Data ;

``````DATA Have;
Length Variable_ID \$ 3 Variable \$ 3;
Infile Datalines Missover Dlm=",";
Input Variable_ID Variable;
Datalines;
001,TUR
001,TUR
002,TUR
002,TUR
003,ENG
003,ENG
004,GER
004,GER
005,FRA
005,FRA
;
Run;
PROC SQL;
Create Table Want As
Select
Variable,
Count(1) as CountOfVariable
/*Calculated CVariable/100 as Percent format=percent8.2*/
From Have
group by Variable;
QUIT;
``````

Desired Output;

Accepted Solutions
Solution
‎01-26-2016 12:22 PM
Super User
Posts: 10,609

## Re: How to Calculate Percentage with a PROC SQL

I am not quit sure what you are trying to do .

``````DATA Have;
Length Variable_ID \$ 3 Variable \$ 3 Number 8;
Infile Datalines Missover Dlm=",";
Input Variable_ID Variable Number;
Datalines;
001,TUR,1
001,TUR,1
002,TUR,0
002,TUR,0
003,ENG,1
003,ENG,1
004,GER,1
004,GER,1
005,FRA,1
005,FRA,1
;
Run;
DATA Have2;
Length Variable \$ 3 Number 8;
Infile Datalines Missover Dlm=",";
Input Variable Number;
Datalines;
TUR,1
TUR,1
TUR,0
TUR,0
ENG,1
ENG,1
GER,1
GER,1
FRA,1
FRA,1
;
Run;
PROC SQL;
CREATE TABLE Want AS
SELECT
H1.Variable,
COUNT(*)/(SELECT count(*)  FROM Have H1
INNER JOIN Have2 H2
ON H1.Variable=H2.Variable
WHERE H2.Number=1 AND H1.Number=1 ) AS Percent FORMAT=Percent8.2
FROM Have H1
INNER JOIN Have2 H2
ON H1.Variable=H2.Variable
WHERE H2.Number=1
AND H1.Number=1
group BY H1.Variable;
QUIT;``````

All Replies
Posts: 1,146

## Re: How to Calculate Percentage with a PROC SQL

``````proc sql;
select count(*) into: obs from have;
create table test as select variable, (count(variable)/&obs) as var format=percent8.2, count(*) as count from have group by variable;
quit;
``````
Thanks,
Jag
Super Contributor
Posts: 490

## Re: How to Calculate Percentage with a PROC SQL

``````PROC SQL noprint;
select nobs into :nobs from dictionary.tables
where libname="WORK" and memname="HAVE";

Create Table Want As
Select
Variable, Count(1)/&nobs as Percent format=percent8.2
From Have
group by Variable;
QUIT;``````
Super User
Posts: 10,609

## Re: How to Calculate Percentage with a PROC SQL

``````DATA Have;
Length Variable_ID \$ 3 Variable \$ 3;
Infile Datalines Missover Dlm=",";
Input Variable_ID Variable;
Datalines;
001,TUR
001,TUR
002,TUR
002,TUR
003,ENG
003,ENG
004,GER
004,GER
005,FRA
005,FRA
;
Run;
PROC SQL;
Create Table Want As
Select
Variable,
count(*)/(select count(*) from have) as Percent format=percent8.2
From Have
group by Variable;
QUIT;``````
Super Contributor
Posts: 413

## Re: How to Calculate Percentage with a PROC SQL

[ Edited ]

Hello,

Thank you all of them,I missed out joining with another table while i did this process according to the following conditions.

Conditions

Have1.Number=1
Have2.Number=1

`DATA Have;Length Variable_ID \$ 3 Variable \$ 3 Number 8;Infile Datalines Missover Dlm=",";Input Variable_ID Variable Number;Datalines;001,TUR,1001,TUR,1002,TUR,0002,TUR,0003,ENG,1003,ENG,1004,GER,1004,GER,1005,FRA,1005,FRA,1;Run;DATA Have2;Length Variable \$ 3 Number 8;Infile Datalines Missover Dlm=",";Input Variable Number;Datalines;TUR,1TUR,1TUR,0TUR,0ENG,1ENG,1GER,1GER,1FRA,1FRA,1;Run;PROC SQL;CREATE TABLE Want ASSELECT H1.Variable,COUNT(*)/(SELECT count(*) FROM Have) AS Percent FORMAT=Percent8.2FROM Have H1 INNER JOIN Have2 H2ON H1.Variable=H2.VariableWHERE H2.Number=1AND H1.Number=1ORDER BY H1.Variable;QUIT;`

Now my desired output as below, Do you have an idea ?

Thanks.

Solution
‎01-26-2016 12:22 PM
Super User
Posts: 10,609

## Re: How to Calculate Percentage with a PROC SQL

I am not quit sure what you are trying to do .

``````DATA Have;
Length Variable_ID \$ 3 Variable \$ 3 Number 8;
Infile Datalines Missover Dlm=",";
Input Variable_ID Variable Number;
Datalines;
001,TUR,1
001,TUR,1
002,TUR,0
002,TUR,0
003,ENG,1
003,ENG,1
004,GER,1
004,GER,1
005,FRA,1
005,FRA,1
;
Run;
DATA Have2;
Length Variable \$ 3 Number 8;
Infile Datalines Missover Dlm=",";
Input Variable Number;
Datalines;
TUR,1
TUR,1
TUR,0
TUR,0
ENG,1
ENG,1
GER,1
GER,1
FRA,1
FRA,1
;
Run;
PROC SQL;
CREATE TABLE Want AS
SELECT
H1.Variable,
COUNT(*)/(SELECT count(*)  FROM Have H1
INNER JOIN Have2 H2
ON H1.Variable=H2.Variable
WHERE H2.Number=1 AND H1.Number=1 ) AS Percent FORMAT=Percent8.2
FROM Have H1
INNER JOIN Have2 H2
ON H1.Variable=H2.Variable
WHERE H2.Number=1
AND H1.Number=1
group BY H1.Variable;
QUIT;``````
Super User
Posts: 9,193

## Re: How to Calculate Percentage with a PROC SQL

```data have;
length variable_id \$ 3 variable \$ 3 number 8;
infile datalines missover dlm=",";
input variable_id variable number;
datalines;
001,TUR,1
001,TUR,1
002,TUR,0
002,TUR,0
003,ENG,1
003,ENG,1
004,GER,1
004,GER,1
005,FRA,1
005,FRA,1
;
run;
data have2;
length variable \$ 3 number 8;
infile datalines missover dlm=",";
input variable number;
datalines;
TUR,1
TUR,1
TUR,0
TUR,0
ENG,1
ENG,1
GER,1
GER,1
FRA,1
FRA,1
;
run;

data inter;
set have (drop=variable_id) have2;
if number;
run;

proc sql;
create table WANT as
select  distinct
A.VARIABLE,
(select count(VARIABLE) from INTER where VARIABLE=A.VARIABLE) / (select count(VARIABLE) from INTER) as PERCENT format=percent8.2
from    INTER A;
quit;```
Super User
Posts: 5,824

## Re: How to Calculate Percentage with a PROC SQL

It's fun to see how to solve things in different ways.

But, as long as we are in the SAS world, remember to use the best tool for the situation, and for this situation PROC FREQ is better suited (IMHO) than SQL.

Data never sleeps
Super User
Posts: 9,193

## Re: How to Calculate Percentage with a PROC SQL

Yes, but how often is it used...

/humour

Super Contributor
Posts: 413

## Re: How to Calculate Percentage with a PROC SQL

Thank you all for helping me

Occasional Contributor
Posts: 5

## Re: How to Calculate Percentage with a PROC SQL

This problem can be simplified by using a subquery for the count of total number of variable as :

DATA Have;
Length Variable_ID \$ 3 Variable \$ 3;
Infile Datalines Missover Dlm=",";
Input Variable_ID Variable;
Datalines;
001,TUR
001,TUR
002,TUR
002,TUR
003,ENG
003,ENG
004,GER
004,GER
005,FRA
005,FRA
;
Run;

Proc sql;

create table want as
select Variable, (Count(Variable) / (Select Count(Variable) from have)) * 100 as Percent
from have
group by Variable;

quit;

☑ This topic is solved.