<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to Calculate Percentage with a PROC SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245849#M45916</link>
    <description>&lt;P&gt;How about:&lt;/P&gt;
&lt;PRE&gt;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;&lt;/PRE&gt;</description>
    <pubDate>Mon, 25 Jan 2016 10:59:29 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2016-01-25T10:59:29Z</dc:date>
    <item>
      <title>How to Calculate Percentage with a PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245765#M45883</link>
      <description>&lt;P&gt;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 ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sample Data ;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Desired Output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/1598i019D506C1C768EFB/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Desired.png" title="Desired.png" /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jan 2016 00:08:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245765#M45883</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2016-01-25T00:08:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to Calculate Percentage with a PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245772#M45886</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select count(*) into: obs from have; 
create table test as select variable, (count(variable)/&amp;amp;obs) as var format=percent8.2, count(*) as count from have group by variable;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jan 2016 00:51:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245772#M45886</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2016-01-25T00:51:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to Calculate Percentage with a PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245773#M45887</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL noprint;
select nobs into :nobs from dictionary.tables 
where libname="WORK" and memname="HAVE";

Create Table Want As
Select
Variable, Count(1)/&amp;amp;nobs as Percent format=percent8.2
From Have
group by Variable;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jan 2016 00:52:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245773#M45887</guid>
      <dc:creator>mohamed_zaki</dc:creator>
      <dc:date>2016-01-25T00:52:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to Calculate Percentage with a PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245774#M45888</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jan 2016 00:58:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245774#M45888</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-01-25T00:58:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to Calculate Percentage with a PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245823#M45910</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you all of them,I&amp;nbsp;missed out joining with another table while i did this process according to the following conditions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Conditions&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;Have1.Number=1&lt;BR /&gt;Have2.Number=1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA Have;&lt;BR /&gt;Length Variable_ID $ 3 Variable $ 3 Number 8;&lt;BR /&gt;Infile Datalines Missover Dlm=",";&lt;BR /&gt;Input Variable_ID Variable Number;&lt;BR /&gt;Datalines;&lt;BR /&gt;001,TUR,1&lt;BR /&gt;001,TUR,1&lt;BR /&gt;002,TUR,0&lt;BR /&gt;002,TUR,0&lt;BR /&gt;003,ENG,1&lt;BR /&gt;003,ENG,1&lt;BR /&gt;004,GER,1&lt;BR /&gt;004,GER,1&lt;BR /&gt;005,FRA,1&lt;BR /&gt;005,FRA,1&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;BR /&gt;DATA Have2;&lt;BR /&gt;Length Variable $ 3 Number 8;&lt;BR /&gt;Infile Datalines Missover Dlm=",";&lt;BR /&gt;Input Variable Number;&lt;BR /&gt;Datalines;&lt;BR /&gt;TUR,1&lt;BR /&gt;TUR,1&lt;BR /&gt;TUR,0&lt;BR /&gt;TUR,0&lt;BR /&gt;ENG,1&lt;BR /&gt;ENG,1&lt;BR /&gt;GER,1&lt;BR /&gt;GER,1&lt;BR /&gt;FRA,1&lt;BR /&gt;FRA,1&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;CREATE TABLE Want AS&lt;BR /&gt;SELECT &lt;BR /&gt;H1.Variable,&lt;BR /&gt;COUNT(*)/(SELECT count(*) FROM Have) AS Percent FORMAT=Percent8.2&lt;BR /&gt;FROM Have H1 &lt;BR /&gt;INNER JOIN Have2 H2&lt;BR /&gt;ON H1.Variable=H2.Variable&lt;BR /&gt;WHERE H2.Number=1&lt;BR /&gt;AND H1.Number=1&lt;BR /&gt;ORDER BY H1.Variable;&lt;BR /&gt;QUIT;&lt;/PRE&gt;
&lt;P&gt;Now my desired output as below, Do you have an idea ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/1603i90D53098573ACCA9/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Desired.png" title="Desired.png" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jan 2016 09:10:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245823#M45910</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2016-01-25T09:10:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to Calculate Percentage with a PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245838#M45913</link>
      <description>&lt;P&gt;I am not quit sure what you are trying to do .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jan 2016 09:54:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245838#M45913</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-01-25T09:54:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to Calculate Percentage with a PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245849#M45916</link>
      <description>&lt;P&gt;How about:&lt;/P&gt;
&lt;PRE&gt;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;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jan 2016 10:59:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245849#M45916</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-01-25T10:59:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to Calculate Percentage with a PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245885#M45933</link>
      <description>&lt;P&gt;It's fun to see how to solve things in different ways.&lt;/P&gt;
&lt;P&gt;But, as long as we are in the SAS world, remember&amp;nbsp;to use the best tool for the situation, and for this situation PROC FREQ is better suited (IMHO) than SQL.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jan 2016 14:55:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245885#M45933</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-01-25T14:55:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to Calculate Percentage with a PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245886#M45934</link>
      <description>&lt;P&gt;Yes, but how often is it used...&lt;/P&gt;
&lt;P&gt;/humour&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jan 2016 14:59:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/245886#M45934</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-01-25T14:59:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to Calculate Percentage with a PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/246152#M46026</link>
      <description>&lt;P&gt;Thank you all for helping me &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 17:31:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/246152#M46026</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2016-01-26T17:31:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to Calculate Percentage with a PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/389386#M93365</link>
      <description>&lt;P&gt;This problem can be simplified by using a subquery for the count of total number of variable as :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA Have;&lt;BR /&gt;Length Variable_ID $ 3 Variable $ 3;&lt;BR /&gt;Infile Datalines Missover Dlm=",";&lt;BR /&gt;Input Variable_ID Variable;&lt;BR /&gt;Datalines;&lt;BR /&gt;001,TUR&lt;BR /&gt;001,TUR&lt;BR /&gt;002,TUR&lt;BR /&gt;002,TUR&lt;BR /&gt;003,ENG&lt;BR /&gt;003,ENG&lt;BR /&gt;004,GER&lt;BR /&gt;004,GER&lt;BR /&gt;005,FRA&lt;BR /&gt;005,FRA&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc sql;&lt;/P&gt;&lt;P&gt;create table want as&lt;BR /&gt;select Variable, (Count(Variable) / (Select Count(Variable) from have)) * 100 as Percent&lt;BR /&gt;from have&lt;BR /&gt;group by Variable;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Aug 2017 09:01:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/389386#M93365</guid>
      <dc:creator>Harmandeep</dc:creator>
      <dc:date>2017-08-20T09:01:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to Calculate Percentage with a PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/481626#M124615</link>
      <description>I like your "sass"!</description>
      <pubDate>Thu, 26 Jul 2018 18:05:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/481626#M124615</guid>
      <dc:creator>amandaokello</dc:creator>
      <dc:date>2018-07-26T18:05:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to Calculate Percentage with a PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/481645#M124622</link>
      <description>&lt;P&gt;I feel the solutions provided by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;&amp;nbsp;and also by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49185"&gt;@mohamed_zaki&lt;/a&gt;&amp;nbsp;could give the wrong result in some situations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there are missing values in variable, then the variable &amp;amp;obs will count the missings, and then in the next line, (count(variable)/&amp;amp;obs) will have the wrong denominator.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 18:54:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Percentage-with-a-PROC-SQL/m-p/481645#M124622</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-07-26T18:54:37Z</dc:date>
    </item>
  </channel>
</rss>

