SAS Enterprise Guide

Desktop productivity for business analysts and programmers
BookmarkSubscribeRSS Feed
nbknoid
Calcite | Level 5

Currently takes about 1.5 hours to run.

 

PROC SQL;
CREATE TABLE WORK.QUERY_MedicalTable AS

SELECT DISTINCT
(CASE WHEN MISSING(SUBSTR(t1.SVCCD,1,3)) THEN t2.SOURCE2 ELSE (SUBSTR(t1.SVCCD,1,3)) END) AS SOURCE
,t1.LOC_ID_CD AS PRIOR_LOCATION
,(SUM(t1.SVC_UNT_QT)/3) AS PRIOR_QUANTITY_AVG
,t2.LOC_ID_CD AS CURRENT_MONTH_LOCATIONS
,t2.CURRENT_MONTH_QUANTITY
,((t2.CURRENT_MONTH_QUANTITY - (SUM(t1.SVC_UNT_QT)/3))/ (SUM(t1.SVC_UNT_QT))) AS VARIANCE
FROM MedicalTable t1
FULL JOIN (SELECT DISTINCT
SUBSTR(t2a.SVCCD,1,3) AS SOURCE2
,t2a.LOC_ID_CD
,SUM(t2a.SVC_UNT_QT) AS CURRENT_MONTH_QUANTITY
FROM MedicalTable t2a
WHERE t2a.YR_NB = 2019 AND t2a.MM_NB = 4  
GROUP BY SUBSTR(t2a.SVCCD,1,3), t2a.LOC_ID_CD) t2
ON (t1.LOC_ID_CD = t2.LOC_ID_CD AND SUBSTR(t1.SVCCD,1,3) = t2.SOURCE2)
WHERE t1.YR_NB = 2019 AND t1.MM_NB BETWEEN 1 AND 3  
GROUP BY SUBSTR(t1.SVCCD,1,3), t1.LOC_ID_CD, t2.LOC_ID_CD, t2.SOURCE2
HAVING (((t2.CURRENT_MONTH_QUANTITY - (SUM(t1.SVC_UNT_QT)/3))/ (SUM(t1.SVC_UNT_QT))) NOT BETWEEN .0000001 AND .049999)
OR (((t2.CURRENT_MONTH_QUANTITY - (SUM(t1.SVC_UNT_QT)/3))/ (SUM(t1.SVC_UNT_QT))) NOT BETWEEN -.0000001 AND -.049999)
ORDER BY SUBSTR(t1.SVCCD,1,3) ASC , t1.LOC_ID_CD ASC;

QUIT;

2 REPLIES 2
SASKiwi
PROC Star

Welcome to the SAS Community.

 

There's a lot going on in your query and you seem to have only one source SAS table. How many rows does it have?

 

I suggest you start by removing the sub-query join and time how long the remaining query takes.

 

Due you really need to use DISTINCT? If your GROUP BY's are working correctly these shouldn't be necessary.

LinusH
Tourmaline | Level 20

Also, provide the log with FULLSTIMER option enabled, where you can see whether your performance is I/O or CPU bound.

If you have any indexes use MSGLEVEL=I to see if any is being used.

Executing SQL with _method gives hints on how PROC SQL is evaluating and plan the query.

Data never sleeps

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 914 views
  • 1 like
  • 3 in conversation