I am using Proc SQL within SAS.
When I use the GROUP BY statement in my main query, I get this error:
NOTE: A GROUP BY clause has been discarded because neither the SELECT clause nor the optional HAVING clause of the associated table-expression referenced a summary function.
SELECT
HP.Area
,HP.Name
,HP.NPI
,FACILITIES.ID
,(SELECT COUNT(*) FROM EVAL.CITATIONS C
WHERE C.ID = CITATIONS.ID
) AS Total_Citations
FROM EVAL.HP HP
LEFT JOIN EVAL.FACILITIES FACILITIES
ON FACILITIES.NPI = HP.NPI
LEFT JOIN EVAL.CITATIONS CITATIONS
ON CITATIONS.ID = FACILITIES.ID
GROUP BY CITATIONS.ID
When I run this program:
- I get duplicate results
- The Total_Citations counts all rows in the citations table because it's ignoring the group by statement.
Output:
HP.Area | HP.Name | HP.NPI | FACILITIES.ID | Total_Citations |
---|---|---|---|---|
AV | OMG Inc. | 1234 | 001 | 17026 |
AV | OMG Inc. | 1234 | 001 | 17026 |
AV | Why | 1241 | 512 | 17026 |
AV | Why | 1241 | 512 | 17026 |
BP | Dis | 8305 | 643 | 17026 |
BP | Happening | 8221 | 346 | 17026 |
It should look like:
HP.Area | HP.Name | HP.NPI | FACILITIES.ID | Total_Citations |
---|---|---|---|---|
AV | OMG Inc. | 1234 | 001 | 14 |
AV | Why | 1241 | 512 | 0 |
BP | Dis | 8305 | 643 | 0 |
BP | Happening | 8221 | 346 | 36 |
The HP table is my main table and I want to left join FACILITIES and CITATIONS tables. FACILITES has the unique identifier (NPI) that connects HP and CITATIONS tables together. CITATIONS has a row for each citation for every facility for a given time period. I am trying to get the total number of citations from CITATIONS per ID.