<?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 can I streamline this code in Administration and Deployment</title>
    <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656450#M19177</link>
    <description>&lt;P&gt;You can replace CASE WHEN ELSE for 0/1 coding such as you show with&lt;/P&gt;
&lt;PRE&gt; CASE WHENT.hcpcs IN(&amp;amp;radOncCPT) THEN1ELSE0END ASflag_radOnc&lt;/PRE&gt;
&lt;P&gt;(the stupid spacing is because of copy from Docx with another viewer, Code belongs in code boxes on the forum opened with the &amp;lt;/&amp;gt; or "running man" icon and paste from the editor or log directly. DOCX and similar files will sometimes change characters such as quotes to non-programming versions)&lt;/P&gt;
&lt;P&gt;with&lt;/P&gt;
&lt;PRE&gt;( t.hcpcs in(&amp;amp;radOncCPT) ) as Flag_radOnc&lt;/PRE&gt;
&lt;P&gt;SAS will return 1 for true and 0 for false for logical comparisons.&lt;/P&gt;</description>
    <pubDate>Wed, 10 Jun 2020 15:18:33 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-06-10T15:18:33Z</dc:date>
    <item>
      <title>How can I streamline this code</title>
      <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656354#M19153</link>
      <description>&lt;P&gt;Good morning all&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not sure if this can be solved without data, but I have been tasked with trying to reduce the run time of the attached SAS code (highlighted in yellow). As you can see in the attached log, all of the coding processes quickly accept the yellow highlighted portion, which takes 45 mins to run. Is there anyway I can cut down the process time on that section of the coding? Any assistance would be greatly appreciated.&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;William&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2020 11:11:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656354#M19153</guid>
      <dc:creator>wheddingsjr</dc:creator>
      <dc:date>2020-06-10T11:11:46Z</dc:date>
    </item>
    <item>
      <title>Re: How can I streamline this code</title>
      <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656437#M19171</link>
      <description>&lt;P&gt;That's a complicated PROC SQL statement: using DISTINCT for a long list of variables, multiple CASE WHEN statements, four LEFT JOINs with datasets from different SQL servers, and a nested SELECT within the WHERE clause.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know the size or scope of the datasets you're using, but here are a couple suggestions to try:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;First, insource the&amp;nbsp;&lt;EM&gt;needed&amp;nbsp;&lt;/EM&gt;data into WORK before doing the LEFT JOINs. Then use these in the LEFT JOINs. That should perform better.&lt;/LI&gt;&lt;LI&gt;Determine whether you need DISTINCTs or not. See if these could be added to the insourcing in #1.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;I've attached a (rough) attempt at that. It likely needs some editing, but hopefully the idea makes sense.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, see&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;'s reply to a related question:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/SAS-SQL-Code-Efficiency-Left-Join/m-p/399490#M96766" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/SAS-SQL-Code-Efficiency-Left-Join/m-p/399490#M96766&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope that helps!&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2020 14:53:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656437#M19171</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2020-06-10T14:53:09Z</dc:date>
    </item>
    <item>
      <title>Re: How can I streamline this code</title>
      <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656440#M19173</link>
      <description>&lt;P&gt;1. There are four left joins and left join is not one of the most ideal way to do joins. See if you can filter the data from the biggest join and then use that reduced set to do the other joins. You can also create four separate sets first and then do the joins&lt;/P&gt;
&lt;P&gt;2. There are lot of case when statements so see if you can split that step.&lt;/P&gt;
&lt;P&gt;3. In the end there is a group by and an order by&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the above is causing the data to run for a long time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2020 14:55:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656440#M19173</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-06-10T14:55:46Z</dc:date>
    </item>
    <item>
      <title>Re: How can I streamline this code</title>
      <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656450#M19177</link>
      <description>&lt;P&gt;You can replace CASE WHEN ELSE for 0/1 coding such as you show with&lt;/P&gt;
&lt;PRE&gt; CASE WHENT.hcpcs IN(&amp;amp;radOncCPT) THEN1ELSE0END ASflag_radOnc&lt;/PRE&gt;
&lt;P&gt;(the stupid spacing is because of copy from Docx with another viewer, Code belongs in code boxes on the forum opened with the &amp;lt;/&amp;gt; or "running man" icon and paste from the editor or log directly. DOCX and similar files will sometimes change characters such as quotes to non-programming versions)&lt;/P&gt;
&lt;P&gt;with&lt;/P&gt;
&lt;PRE&gt;( t.hcpcs in(&amp;amp;radOncCPT) ) as Flag_radOnc&lt;/PRE&gt;
&lt;P&gt;SAS will return 1 for true and 0 for false for logical comparisons.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2020 15:18:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656450#M19177</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-06-10T15:18:33Z</dc:date>
    </item>
    <item>
      <title>Re: How can I streamline this code</title>
      <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656455#M19178</link>
      <description>&lt;P&gt;Please post logs by copy/pasting into a window opened with this button:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg"&gt;&lt;img src="https://communities.sas.com/skins/images/2FD96521DCF95C42FE57BF2A7CB72678/responsive_peak/images/image_not_found.png" alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;for code, use the "little running man" right next to it.&lt;/P&gt;
&lt;P&gt;Only post the step in question, this should suffice.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is the DISTINCT in the SQL really necessary? And have you checked that the GROUP BY is complete, because you have an automatic remerge?&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2020 15:33:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656455#M19178</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-10T15:33:34Z</dc:date>
    </item>
    <item>
      <title>Re: How can I streamline this code</title>
      <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656532#M19179</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114971"&gt;@wheddingsjr&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems like your query is using SAS functions which may not be recognized by the SQL Server!!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	, CASE WHEN datepart(input(dischargedts,anydtdtm.)) BETWEEN intnx('year', "&amp;amp;begDt"D , -1, 'same') AND intnx('day', "&amp;amp;begDt"D , -1, 'same') THEN 0 /* Claims data for year prior to report start date */
     	WHEN datepart(input(dischargedts,anydtdtm.)) BETWEEN "&amp;amp;begDt"D AND "&amp;amp;endDt"D THEN 1
	 	ELSE 2 END AS period

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;DATEPART, INTNX are SAS functions!! This causes the data to transferred to SAS before it is processed!!!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use the following option to see what's going on&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;OPTIONS DEBUG=DBMS_SELECT SASTRACE=',,,d' SASTRACELOC=SASLOG NOSTSUFFIX;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps you&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2020 19:24:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656532#M19179</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2020-06-10T19:24:43Z</dc:date>
    </item>
    <item>
      <title>Re: How can I streamline this code</title>
      <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656534#M19180</link>
      <description>Check out this supporting paper&lt;BR /&gt;&lt;A href="https://www.mwsug.org/proceedings/2015/BB/MWSUG-2015-BB-03.pdf" target="_blank"&gt;https://www.mwsug.org/proceedings/2015/BB/MWSUG-2015-BB-03.pdf&lt;/A&gt;</description>
      <pubDate>Wed, 10 Jun 2020 19:31:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656534#M19180</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2020-06-10T19:31:16Z</dc:date>
    </item>
    <item>
      <title>Re: How can I streamline this code</title>
      <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656563#M19181</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114971"&gt;@wheddingsjr&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try these statements and query, and see if helps you&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
OPTIONS DEBUG=DBMS_SELECT SASTRACE=',,,d' SASTRACELOC=SASLOG NOSTSUFFIX FULLSTIMER;

/* Calculate required date macros */
%let priorYrDt  = %SYSFUNC(PUTN(%SYSFUNC(INTNX(year, "&amp;amp;begDt"d ,-1, same)),date9.));
%let priorDayDt = %SYSFUNC(PUTN(%SYSFUNC(INTNX(day,  "&amp;amp;begDt"D ,-1, same)),date9.));
%put &amp;amp;=priorYrDt &amp;amp;=priorDayDt;

PROC SQL;
	CREATE TABLE work.Datapull_epic AS
	SELECT DISTINCT
	  H.PatientMRN as MRN
	, H.HospitalAccountID as enc
	, H.PrimaryPayorID as PayorID
	, C.Subscriberid
	, C.PlanID
	/*
	, DATEPART(H.DischargeDTS)
	, MAX(H.DISCHARGEDTS) AS LAST_AGENT_DT
	*/
	, H.DISCHARGEDTS 
	, T.HospitalAccountClassDSC
	/*
	, PT.PatientNM */ /* Used PatientFirstNM and PatientLastNM from SharedLocal YM0214 
	*/
	, PT.PatientFirstNM
	, PT.PatientLastNM
	, PT.BIRTHDTS
	, P.PayorNM
	, (T.hcpcs IN (&amp;amp;examCPT)) AS flag_exam /* any exam*/
	, (T.hcpcs IN (&amp;amp;infuCPT) OR T.CPT IN (&amp;amp;infuCPT)) AS flag_infu /* infusion*/
	, (T.hcpcs IN (&amp;amp;chemoCPT) OR T.CPT IN (&amp;amp;chemoCPT)) AS flag_chemo /* chemotherapy*/
	, (T.hcpcs IN (&amp;amp;radOncCPT)) AS flag_radOnc
	, (T.hcpcs IN ('C9027','J9271','C9453','J9299','C9284','J9228','C9399','C9483','J9022','C9491','J9023','C9492')) AS flag_immuno /* immunotherapy */
	, CASE	WHEN dischargedts BETWEEN "&amp;amp;priorYrDt"D AND "&amp;amp;priorDayDt"D THEN 0 /* Claims data for year prior to report start date */
			WHEN dischargedts BETWEEN "&amp;amp;begDt"D AND "&amp;amp;endDt"D THEN 1
			ELSE 2 
	  END AS period
	, SUM(CASE WHEN T.ChargeModifierListTXT LIKE '%DFP%' THEN 0 ELSE INT(T.TransactionCNT) END) AS units
	, SUM(H.TotalChargeAMT) AS charges

	FROM Fin.HospitalAccount H
	LEFT JOIN Fin.Coverage C ON H.CoverageID = C.CoverageID
	LEFT JOIN Ref.Payor P ON P.PayorID = C.PayorID
	LEFT JOIN Fin.HospitalTransaction T ON H.HospitalAccountID = T.HospitalAccountID
	/*INNER JOIN Pat.PATIENT PT ON PT.MRN=H.PATIENTMRN*/ /* YM0214 - changed to left join SharedLocal.Person.PatientExtension below */
	LEFT JOIN Pext.PatientExtension PT ON H.PATIENTMRN = PT.DFCIMRN

	WHERE  
	dischargedts BETWEEN "&amp;amp;priorYrDt"D AND "&amp;amp;endDt"D /* extract claims from 1 year prior to report start date until report end date */
	AND H.PatientMRN IN 
		(SELECT DISTINCT A.PatientMRN
			FROM Fin.HospitalAccount A
			WHERE dischargedts BETWEEN "&amp;amp;begDt"D AND "&amp;amp;endDt"D 
			AND A.PatientMRN NE '') /* for mrn in cohort 1 period*/
	AND T.HospitalAccountClassDSC = 'Outpatient'	/* outpatient only */
	AND ((T.hcpcs NE '') OR H.PatientMRN NE '')

	GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
	ORDER BY H.PatientMRN, period DESC;
QUIT;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Good luck,&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2020 20:58:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/656563#M19181</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2020-06-10T20:58:42Z</dc:date>
    </item>
    <item>
      <title>Re: How can I streamline this code</title>
      <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/657506#M19194</link>
      <description>Thanks MK, sorry for just responding but was away for a few days. I will give that a try and see how it works.</description>
      <pubDate>Thu, 11 Jun 2020 13:19:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/657506#M19194</guid>
      <dc:creator>wheddingsjr</dc:creator>
      <dc:date>2020-06-11T13:19:34Z</dc:date>
    </item>
    <item>
      <title>Re: How can I streamline this code</title>
      <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/657509#M19195</link>
      <description>Thx Smantha. I inherited this code. Someone else wrote and used it for years from what I understand. I get hired and one of the first things I get tasked with is trying to streamline it (as if no one else could do it before I was brought onboard).</description>
      <pubDate>Thu, 11 Jun 2020 13:24:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/657509#M19195</guid>
      <dc:creator>wheddingsjr</dc:creator>
      <dc:date>2020-06-11T13:24:16Z</dc:date>
    </item>
    <item>
      <title>Re: How can I streamline this code</title>
      <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/657510#M19196</link>
      <description>Thanks Ballard, I will try this as well as some of the other suggestions.</description>
      <pubDate>Thu, 11 Jun 2020 13:25:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/657510#M19196</guid>
      <dc:creator>wheddingsjr</dc:creator>
      <dc:date>2020-06-11T13:25:25Z</dc:date>
    </item>
    <item>
      <title>Re: How can I streamline this code</title>
      <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/657511#M19197</link>
      <description>Thanks Kurt for the heads up about posting code. I started to just post the particular part in question but wanted to show exactly what it was doing. I asked abut the "DISTINCT" and was told it was necessary and I have absolutely no idea what a automatic remerge is LOL. (I am a novice at this).</description>
      <pubDate>Thu, 11 Jun 2020 13:28:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/657511#M19197</guid>
      <dc:creator>wheddingsjr</dc:creator>
      <dc:date>2020-06-11T13:28:36Z</dc:date>
    </item>
    <item>
      <title>Re: How can I streamline this code</title>
      <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/657513#M19198</link>
      <description>Thanks Ahmed, I will give it a shout.</description>
      <pubDate>Thu, 11 Jun 2020 13:32:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/657513#M19198</guid>
      <dc:creator>wheddingsjr</dc:creator>
      <dc:date>2020-06-11T13:32:07Z</dc:date>
    </item>
    <item>
      <title>Re: How can I streamline this code</title>
      <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/658249#M19210</link>
      <description>&lt;P&gt;In standard SQL, you usually cannot do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select name, sex, avg(weight)
from class
group by sex&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;because all columns must either be part of the GROUP BY or the result of a summary function. SAS SQL, OTOH, allows this; it calculates the summary functions and then merges the result back into the &lt;EM&gt;whole&lt;/EM&gt; dataset.&lt;/P&gt;
&lt;P&gt;Since this is often not what was intended, a NOTE about remerging is usually an alarm signal that the GROUP BY statement is somehow wrong, e.g. someone added a variable to the SELECT and forgot to add it to the GROUP BY.&amp;nbsp;Or someone used position numbers in the GROUP BY (as is the case with your code), and moving or adding a column shifted everything.&lt;/P&gt;</description>
      <pubDate>Sat, 13 Jun 2020 06:30:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/658249#M19210</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-13T06:30:29Z</dc:date>
    </item>
    <item>
      <title>Re: How can I streamline this code</title>
      <link>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/658262#M19211</link>
      <description>&lt;P&gt;Looking at your log there is a big gap between real time and CPU time. This could be because there is a lot happening on the SQL Server side and SAS is just waiting or because a lot of time gets spent transferring the data from SQL Server to SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now looking at the code you've posted there are function in it which SAS can't push to the database and for this reason a lot of data will get transferred to SAS for processing ( functions intnx() and input() being the main "problem").&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code also combines Left Join On Clauses with Where Clauses. The Where Clause is likely going to post filter the result set from the Join. It would likely be more efficient to first filter the rows in the source tables to only use the rows for joining which are relevant.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here my stab at your code. I had to do this "blind" so not very convinced that this will just work (syntax and logic) - but it hopefully will give you some ideas how you could performance tweak the code.&lt;/P&gt;
&lt;P&gt;I'm still doing the Group By statement on the SAS side. This because there are variables in the Select clause which are not used for either grouping or aggregation - that's something SAS allows you to do but SQL Server would throw an error. Ideally you don't have such additional variables and can do everything on the SQL Server side.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;OPTIONS DEBUG=DBMS_SELECT SASTRACE=',,,d' SASTRACELOC=SASLOG NOSTSUFFIX FULLSTIMER;

/* INITIALIZE GLOBAL VARIABLESS */
%LET begDt= 01MAY2019;  /* reporting begin date; FORMAT= DDMMMYYYY*/
%LET endDt= %sysfunc(intnx(month, "&amp;amp;begDt"D , +11, e),date9.);  /* reporting end date: FORMAT= DDMMMYYYY*/

%LET begDt_prev=%sysfunc(intnx(month, "&amp;amp;begDt"D , -12, b),date9.);
%LET endDt_prev=%sysfunc(intnx(month, "&amp;amp;endDt"D , -12, e),date9.);

%let sqlsrv_begDt=%unquote(%nrbquote(')&amp;amp;begDt%nrbquote('));
%let sqlsrv_endDt=%unquote(%nrbquote(')&amp;amp;endDt%nrbquote('));
%let sqlsrv_begDt_prev=%unquote(%nrbquote(')&amp;amp;begDt_prev%nrbquote('));
%let sqlsrv_endDt_prev=%unquote(%nrbquote(')&amp;amp;endDt_prev%nrbquote('));

%put &amp;amp;=begDt;
%put &amp;amp;=endDt;
%put &amp;amp;=begDt_prev;
%put &amp;amp;=endDt_prev;

%put &amp;amp;=sqlsrv_begDt;
%put &amp;amp;=sqlsrv_endDt;
%put &amp;amp;=sqlsrv_begDt_prev;
%put &amp;amp;=sqlsrv_endDt_prev;

LIBNAME sqldb odbc complete="driver=SQL Server; database=Epic; server=PHSSQL2195" schema=Finance readbuff=2000 read_lock_type=nolock;

PROC SQL;
  connect using sqldb;
  CREATE TABLE work.Datapull_epic AS
  select distinct * 
  from connection to sqldb
  (
    SELECT
      H.PatientMRN as MRN
      , H.HospitalAccountID as enc
      , H.PrimaryPayorID as PayorID
      , C.Subscriberid
      , C.PlanID
      , H.DISCHARGEDTS 
      , T.HospitalAccountClassDSC
      , PT.PatientFirstNM
      , PT.PatientLastNM
      , PT.BIRTHDTS
      , P.PayorNM
      , T.flag_exam /** any exam**/
      , T.flag_infu /** infusion**/
      , T.flag_chemo /** chemotherapy**/
      , T.flag_radOnc
      , T.flag_immuno /** immunotherapy */
      , H.period
      , SUM(T.Units) AS units
      , SUM(H.TotalChargeAMT) AS charges

    (
      select 
        PatientMRN as MRN
        , HospitalAccountID 
        , PrimaryPayorID 
        , DISCHARGEDTS
        , TotalChargeAMT
        , CASE 
          WHEN cast(dischargedts as date) 
            BETWEEN &amp;amp;sqlsrv_begDt_prev. AND &amp;amp;sqlsrv_endDt_prev. THEN 0 /* Claims data for year prior to report start date */
          WHEN cast(dischargedts as date) BETWEEN &amp;amp;sqlsrv_begDt. AND &amp;amp;sqlsrv_endDt. THEN 1
          ELSE 2 
          END 
          AS period
      FROM Fin.HospitalAccount 
      where 
        cast(dischargedts as date)  
          between &amp;amp;sqlsrv_begDt_prev. and &amp;amp;sqlsrv_endDt_prev. /* extract claims from 1 year prior to report start date until report end date */
        AND PatientMRN IN 
              (SELECT DISTINCT A.PatientMRN
               FROM Fin.HospitalAccount A
               WHERE cast(dischargedts as date) BETWEEN &amp;amp;sqlsrv_begDt. AND &amp;amp;sqlsrv_endDt. 
              ) /* for mrn in cohort 1 period*/
    ) H

    LEFT JOIN Fin.Coverage C 
      ON H.CoverageID = C.CoverageID

    LEFT JOIN Ref.Payor P 
      ON P.PayorID = C.PayorID

    LEFT JOIN 
      ( select
          T.HospitalAccountClassDSC
          , CASE 
              WHEN T.hcpcs IN (&amp;amp;examCPT) THEN 1 
              ELSE 0 
            END 
            AS flag_exam /** any exam**/
          , CASE WHEN T.hcpcs IN (&amp;amp;infuCPT) OR T.CPT IN (&amp;amp;infuCPT) THEN 1 ELSE 0 END AS flag_infu /** infusion**/
          , CASE WHEN T.hcpcs IN (&amp;amp;chemoCPT) OR T.CPT IN (&amp;amp;chemoCPT) THEN 1 ELSE 0 END AS flag_chemo /** chemotherapy**/
          , CASE 
              WHEN T.hcpcs IN (&amp;amp;radOncCPT) THEN 1 
              ELSE 0 
            END 
            AS flag_radOnc
          , CASE 
              WHEN T.hcpcs IN ('C9027','J9271','C9453','J9299','C9284','J9228','C9399','C9483','J9022','C9491','J9023','C9492') THEN 1 
              ELSE 0 
            END 
            AS flag_immuno /** immunotherapy */
          , CASE 
                WHEN T.ChargeModifierListTXT LIKE '%DFP%' THEN 0 
                ELSE INT(T.TransactionCNT) 
              END
              AS units
          from Fin.HospitalTransaction T 
          where 
            T.HospitalAccountClassDSC = 'Outpatient'  /* outpatient only */
            AND T.hcpcs NE ''
        ) T
      ON H.HospitalAccountID = T.HospitalAccountID

    LEFT JOIN Pext.PatientExtension PT 
      ON H.PATIENTMRN = PT.DFCIMRN
  )
  GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
  ORDER BY H.PatientMRN , period DESC
  ;
QUIT;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jun 2020 02:32:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Administration-and-Deployment/How-can-I-streamline-this-code/m-p/658262#M19211</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-06-14T02:32:34Z</dc:date>
    </item>
  </channel>
</rss>

