calculations in PROC SQL

Solved
Super Contributor
Posts: 286

calculations in PROC SQL

(I know I should know this). I want to get rid of the hard coded number (10021). This number equals the number of records in the data set.

My objective is to count the number of obs in each efaprimary category listed in the WHERE clause and then get a percentage that it represents of the entire data set. In other words, there are some efaprimary codes not included in the report but I need to count those obs to use in the denominator.

proc sql;
SELECT d.efaprimary, count(efaprimary) as Ncount,
calculated Ncount/10021 as Pct format=percent8.2
FROM d45 as d
WHERE enroll_status='0' and grade_level not in ('-1','-2') and efaprimary in ('*DD','*OHI','*PMD','*TBI','AU','EH','EM','HH','LD','OH','SP','TM','VH')
GROUP BY efaprimary;
QUIT;

Accepted Solutions
Solution
‎05-31-2017 10:17 AM
PROC Star
Posts: 8,163

Re: calculations in PROC SQL

One way would be to include the total n as a subquerry. e.g.:

```proc sql;
SELECT d.sex, count(sex) as Ncount,
(select count(*) from sashelp.class) as total,
calculated Ncount/calculated total as Pct format=percent8.2
FROM sashelp.class as d
WHERE age le 12
GROUP BY sex
;
QUIT;
```

Art, CEO, AnalystFinder.com

All Replies
Super User
Posts: 10,209

Re: calculations in PROC SQL

[ Edited ]
``````proc sql noprint;
select nobs into :nobs from dictionary.tables where libname = 'WORK' and upcase(memname) = 'D45';
quit;``````

Now you can use &nobs instead of 10021.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super Contributor
Posts: 286

Re: calculations in PROC SQL

should "info" be "in" ?

Super User
Posts: 10,209

Re: calculations in PROC SQL

GreggB wrote:

should "info" be "in" ?

Actually, it should be "into". Typo, fixed it.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎05-31-2017 10:17 AM
PROC Star
Posts: 8,163

Re: calculations in PROC SQL

One way would be to include the total n as a subquerry. e.g.:

```proc sql;
SELECT d.sex, count(sex) as Ncount,
(select count(*) from sashelp.class) as total,
calculated Ncount/calculated total as Pct format=percent8.2
FROM sashelp.class as d
WHERE age le 12
GROUP BY sex
;
QUIT;
```

Art, CEO, AnalystFinder.com

Super Contributor
Posts: 286

Re: calculations in PROC SQL

Thanks.

☑ This topic is solved.