DATA Step, Macro, Functions and more

How to Calculate Percentage with a PROC SQL

Accepted Solution Solved
Reply
Super Contributor
Posts: 381
Accepted Solution

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;

 

Desired.png


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

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;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,128

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: 9,676

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: 381

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,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) 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
ORDER BY H1.Variable;
QUIT;

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

 

Desired.png

 

Thanks.

 

 

Solution
‎01-26-2016 12:22 PM
Super User
Posts: 9,676

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
Super User
Posts: 7,401

Re: How to Calculate Percentage with a PROC SQL

How about:

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,256

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
Super User
Posts: 7,401

Re: How to Calculate Percentage with a PROC SQL

Yes, but how often is it used...

/humour

Super Contributor
Posts: 381

Re: How to Calculate Percentage with a PROC SQL

Thank you all for helping me Smiley Happy

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 2101 views
  • 6 likes
  • 6 in conversation