<?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: PROC SQL VS DATA STEP FOR IF STATEMENT in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-VS-DATA-STEP-FOR-IF-STATEMENT/m-p/294916#M60096</link>
    <description>&lt;P&gt;YOu are correct about the denominator...The numerator takes dataset FOUR a couple of steps further. Ultimately, the numerator will be the&amp;nbsp;distict count of PERSONID by RX_QTRYR from the resulting dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	CREATE TABLE FIVE AS
		SELECT
			A.PERSONID,
			A.RX_QTRYR,
			B.DT_FILLED ,
			B.DT_COMPLETE
		FROM FOUR A, O_TEST2012 B
		WHERE
			A.PERSONID=B.PERSONID AND
			B.STATE='LA' AND
			A.RX_QTRYR=B.RX_QTRYR AND
			B.DT_FILLED &amp;gt;= A.DT_FILLED 
		ORDER BY A.PERSONID, A.RX_QTRYR
		;
QUIT;

PROC SQL;
	CREATE TABLE SIX AS
		SELECT
			RX_QTRYR,
			COUNT(DISTINCT PERSONID) AS NUM_ON
		FROM FIVE
		GROUP BY RX_QTRYR
		;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 29 Aug 2016 19:24:56 GMT</pubDate>
    <dc:creator>gdaymte</dc:creator>
    <dc:date>2016-08-29T19:24:56Z</dc:date>
    <item>
      <title>PROC SQL VS DATA STEP FOR IF STATEMENT</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-VS-DATA-STEP-FOR-IF-STATEMENT/m-p/294869#M60092</link>
      <description>&lt;P&gt;I am trying to eliminate some extra steps in my SAS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to use the same source data a few times to accurately count certain the numerator and denominator for a particular measure.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first step in the process is to count distinct PERSONID by quarter for records where state=LA. This will be the count for my denominator.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA ONE (KEEP=RX_QTRYR PERSONID); SET O_TEST2012;
     IF STATE='LA';
RUN;

PROC SQL;
     CREATE TABLE ONE_LA AS
     SELECT RX_QTRYR, COUNT(DISTINCT PERSONID) AS NUM_LA
     FROM ONE
     GROUP BY RX_QTRYR
     ;
QUIT;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In order to calculate the numerator, I have to re-sort the original data by PERSONID, DT_FILLED and DT_COMPLETE. Next, I have to use a lag function with a by group and an IF/THEN/DO statement to output only the First PERSONID in each grouping. Then, I to sort the data agian&amp;nbsp;using NODUPKEY by PERSONID and RX_QTRYR.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SORT DATA=O_TEST2012;
	BY PERSONID DT_FILLED DT_COMPLETE;
RUN;

DATA TWO; SET O_TEST2012;

	BY PERSONID DT_FILLED DT_COMPLETE;

	P_DT_COMPLETE=LAG(DT_COMPLETE);
	
	IF FIRST.PERSONID THEN DO;
		OUTPUT;
	END;

	FORMAT P_DT_COMPLETE MMDDYY10.;
RUN;

PROC SORT DATA=THREE OUT=FOUR NODUPKEY;
	BY PERSONID RX_QTRYR;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since I have to calculate the numerator by using the same original dataset, would there be a way to incorporate this distinct count of STATE=LA into the dataset to calculate the numerator from the beginning? I was thinking about using&amp;nbsp;PROC SQL to eliminate some of the sorting, but was hoping to be able to include the counting piece as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	CREATE TABLE ONE_V2 AS
		SELECT
			PERSONID,
			DT_FILLED,
			DT_COMPLETE,
			RX_QTRYR,
			STATE,
			CASE
                             WHEN STATE='LA' THEN COUNT(DISTINCT PERSONID)
                             ELSE 0
                        END AS NUM_LA
		FROM O_TEST2012
		GROUP BY RX_QTRYR
		ORDER BY PERSONID, DT_FILLED, DT_COMPLETE
		;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Unfortunately, that case statement doesn't yield the same results as the count in table ONE_LA.&amp;nbsp;Any assistance would be greatly appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Aug 2016 15:44:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-VS-DATA-STEP-FOR-IF-STATEMENT/m-p/294869#M60092</guid>
      <dc:creator>gdaymte</dc:creator>
      <dc:date>2016-08-29T15:44:42Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL VS DATA STEP FOR IF STATEMENT</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-VS-DATA-STEP-FOR-IF-STATEMENT/m-p/294897#M60095</link>
      <description>&lt;P&gt;So, the denominator is the number of personid in LA in each quarter. What is the numerator?&lt;/P&gt;</description>
      <pubDate>Mon, 29 Aug 2016 17:31:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-VS-DATA-STEP-FOR-IF-STATEMENT/m-p/294897#M60095</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-08-29T17:31:36Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL VS DATA STEP FOR IF STATEMENT</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-VS-DATA-STEP-FOR-IF-STATEMENT/m-p/294916#M60096</link>
      <description>&lt;P&gt;YOu are correct about the denominator...The numerator takes dataset FOUR a couple of steps further. Ultimately, the numerator will be the&amp;nbsp;distict count of PERSONID by RX_QTRYR from the resulting dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	CREATE TABLE FIVE AS
		SELECT
			A.PERSONID,
			A.RX_QTRYR,
			B.DT_FILLED ,
			B.DT_COMPLETE
		FROM FOUR A, O_TEST2012 B
		WHERE
			A.PERSONID=B.PERSONID AND
			B.STATE='LA' AND
			A.RX_QTRYR=B.RX_QTRYR AND
			B.DT_FILLED &amp;gt;= A.DT_FILLED 
		ORDER BY A.PERSONID, A.RX_QTRYR
		;
QUIT;

PROC SQL;
	CREATE TABLE SIX AS
		SELECT
			RX_QTRYR,
			COUNT(DISTINCT PERSONID) AS NUM_ON
		FROM FIVE
		GROUP BY RX_QTRYR
		;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Aug 2016 19:24:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-VS-DATA-STEP-FOR-IF-STATEMENT/m-p/294916#M60096</guid>
      <dc:creator>gdaymte</dc:creator>
      <dc:date>2016-08-29T19:24:56Z</dc:date>
    </item>
  </channel>
</rss>

