<?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 Need to call a SQL query in a data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390796#M93785</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;In order to do some data preparation, I need to run a program like the following, where I can use a SQL SELECT statement within a data step. I am not sure whether this is something doable is SAS or I need to use a higher-level language (e.g., Java) for that.&lt;/P&gt;&lt;P&gt;I appreciate if you could help me out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;//update: I want to assign different values to each hospital visit of a patient based on the time it has passed between their last discharge and the current visit. If they visit much earlier than the hitherto data set average, they will get a larger number showing they are riskier.&lt;/P&gt;&lt;P&gt;I have attached a sample data with desired output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your great support!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data in;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;set out;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;by patient_sk;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if first.patient_sk then prob = (select avg(readmission_time) from out where discharged_dt_tm &amp;lt;= date1); // date1 is the value of the date_var in the current record&lt;BR /&gt;&amp;nbsp; &amp;nbsp;else prob = (admitted_dt_tm - lag(discharged_dt_tm)) / (select avg(readmission_time) from out where discharged_dt_tm &amp;lt;= date1);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 25 Aug 2017 03:11:31 GMT</pubDate>
    <dc:creator>hamed_majidi_gmail_com</dc:creator>
    <dc:date>2017-08-25T03:11:31Z</dc:date>
    <item>
      <title>Need to call a SQL query in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390796#M93785</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;In order to do some data preparation, I need to run a program like the following, where I can use a SQL SELECT statement within a data step. I am not sure whether this is something doable is SAS or I need to use a higher-level language (e.g., Java) for that.&lt;/P&gt;&lt;P&gt;I appreciate if you could help me out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;//update: I want to assign different values to each hospital visit of a patient based on the time it has passed between their last discharge and the current visit. If they visit much earlier than the hitherto data set average, they will get a larger number showing they are riskier.&lt;/P&gt;&lt;P&gt;I have attached a sample data with desired output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your great support!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data in;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;set out;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;by patient_sk;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if first.patient_sk then prob = (select avg(readmission_time) from out where discharged_dt_tm &amp;lt;= date1); // date1 is the value of the date_var in the current record&lt;BR /&gt;&amp;nbsp; &amp;nbsp;else prob = (admitted_dt_tm - lag(discharged_dt_tm)) / (select avg(readmission_time) from out where discharged_dt_tm &amp;lt;= date1);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 03:11:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390796#M93785</guid>
      <dc:creator>hamed_majidi_gmail_com</dc:creator>
      <dc:date>2017-08-25T03:11:31Z</dc:date>
    </item>
    <item>
      <title>Re: Need to call a SQL query in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390798#M93787</link>
      <description>&lt;P&gt;My SQL isn't strong enough to do this, but I think I can give you an approach, based on the strengths of SQL vs. a DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, append the average z value (respecting the date conditions) to every observation. &amp;nbsp;PROC SQL should be able to do that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then go through a DATA step to compute y.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 00:50:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390798#M93787</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-08-25T00:50:40Z</dc:date>
    </item>
    <item>
      <title>Re: Need to call a SQL query in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390802#M93789</link>
      <description>&lt;P&gt;Is the average of z for date_ var &amp;lt;= date1 supposed to be taken over the whole datsaset or only within&amp;nbsp;the current&amp;nbsp;x group?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and are x groups sorted by date_var ?&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 02:06:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390802#M93789</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-08-25T02:06:17Z</dc:date>
    </item>
    <item>
      <title>Re: Need to call a SQL query in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390805#M93792</link>
      <description>&lt;P&gt;Average of z should be taken over the whole data set (in fact a subset of the whole data set that matches with the WHERE clause).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, x groups are sorted by date_var (in ascending order).&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 02:38:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390805#M93792</guid>
      <dc:creator>hamed_majidi_gmail_com</dc:creator>
      <dc:date>2017-08-25T02:38:29Z</dc:date>
    </item>
    <item>
      <title>Re: Need to call a SQL query in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390806#M93793</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for helping. I like your idea, but I think that approach will still need a combination of Proc and data steps. The select statement is not one statement; it varies based on where in the data set the current processing is.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 02:41:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390806#M93793</guid>
      <dc:creator>hamed_majidi_gmail_com</dc:creator>
      <dc:date>2017-08-25T02:41:36Z</dc:date>
    </item>
    <item>
      <title>Re: Need to call a SQL query in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390807#M93794</link>
      <description>&lt;P&gt;Post sample data and expected output please.&amp;nbsp;This is straightforward in multiple steps, but I assume your purpose here is to do it in one step?&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 02:45:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390807#M93794</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-08-25T02:45:26Z</dc:date>
    </item>
    <item>
      <title>Re: Need to call a SQL query in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390809#M93795</link>
      <description>&lt;P&gt;Let's change the names of the dataset to HAVE and WANT since your example code is using IN and OUT in a confusing way.&lt;/P&gt;
&lt;P&gt;You should be able to just calculate the mean value BEFORE starting the data step. &amp;nbsp;If you really want to compare the value of Z to value of Z on the previous record then you need have a variable that orders the rows within the X groups. &amp;nbsp;Let's assume that you want to order them by DATE_VAR.(and that DATE_VAR is unique within values of X).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
create table middle as
  select a.*,(select mean(b.z) from have b where b.date_var &amp;lt;= a.date_var) as avg_z
  from have a
  order by x, date_var
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can use a data step to calculate your new Y variable. &amp;nbsp; Note that you should NOT use the LAG() function in conditionally executed code as it messes up how it keeps track of the previous values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
   set middle;
   by x;
   y = divide(z - lag(z),avg_z );
   if first.x then y = avg_z ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Aug 2017 02:57:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390809#M93795</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-08-25T02:57:44Z</dc:date>
    </item>
    <item>
      <title>Re: Need to call a SQL query in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390810#M93796</link>
      <description>&lt;P&gt;Thank you Reeza. I just updated the question with a sample.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't care in how many steps the job is done. If I can create the final data set, I will be happy!&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 03:13:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390810#M93796</guid>
      <dc:creator>hamed_majidi_gmail_com</dc:creator>
      <dc:date>2017-08-25T03:13:13Z</dc:date>
    </item>
    <item>
      <title>Re: Need to call a SQL query in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390811#M93797</link>
      <description>&lt;P&gt;Here is the sample data for those who prefer csv data&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ENCOUNTER_ID,PATIENT_SK,DISCHARGED_DT_TM,ADMITTED_DT_TM,readmission_time,prob(desired output)
410338226,10000174,2015-04-18T20:30:00,2015-04-13T13:42:00,,avg() of whole data set before 2015-04-18
410730615,10000174,2015-07-16T15:49:00,2015-07-13T15:43:00,85.80069444,85 / avg() before 2015-07-16
205494260,10000214,2012-07-28T18:17:00,2012-07-26T19:43:00,,avg() of whole data set before 2012-07-28
51023730,10000388,2007-02-17T19:18:00,2007-01-29T11:44:00,,avg() of whole data set before 2007-02-17
50974455,10000388,2007-03-07T19:35:00,2007-03-04T16:11:00,14.87013889,14.87 / avg() before 2007-03-07
51216578,10000388,2007-04-19T16:31:00,2007-04-09T06:12:00,32.44236111,32.44 / avg() before 2007-04-19
&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Aug 2017 03:23:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390811#M93797</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-08-25T03:23:49Z</dc:date>
    </item>
    <item>
      <title>Re: Need to call a SQL query in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390814#M93800</link>
      <description>&lt;P&gt;No simple way to do this in a single step. My closest approximation is to define a view:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data out;
infile datalines dsd;
informat DISCHARGED_DT_TM ADMITTED_DT_TM E8601DT.;
format DISCHARGED_DT_TM ADMITTED_DT_TM E8601DT19.;
input ENCOUNTER_ID PATIENT_SK DISCHARGED_DT_TM ADMITTED_DT_TM readmission_time;
z = 10*_n_; /* Some value */
datalines;
410338226,10000174,2015-04-18T20:30:00,2015-04-13T13:42:00,,avg() of whole data set before 2015-04-18
410730615,10000174,2015-07-16T15:49:00,2015-07-13T15:43:00,85.80069444,85 / avg() before 2015-07-16
205494260,10000214,2012-07-28T18:17:00,2012-07-26T19:43:00,,avg() of whole data set before 2012-07-28
51023730,10000388,2007-02-17T19:18:00,2007-01-29T11:44:00,,avg() of whole data set before 2007-02-17
50974455,10000388,2007-03-07T19:35:00,2007-03-04T16:11:00,14.87013889,14.87 / avg() before 2007-03-07
51216578,10000388,2007-04-19T16:31:00,2007-04-09T06:12:00,32.44236111,32.44 / avg() before 2007-04-19
;

proc sql;
create view outView as
select 
	a.PATIENT_SK,
	a.ADMITTED_DT_TM,
	mean(b.z) as avgz
from 
	out as a inner join
	out as b on b.ADMITTED_DT_TM &amp;lt;= a.ADMITTED_DT_TM
group by a.PATIENT_SK, a.ADMITTED_DT_TM;
quit;

data in;
merge out outview;
by PATIENT_SK ADMITTED_DT_TM;
lagz = lag(z);
if first.PATIENT_SK 
	then y = avgz;
	else y = (z-lagz) / avgz;
drop lagz avgz;
run;

proc sql; drop view outView; quit; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Aug 2017 03:50:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/390814#M93800</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-08-25T03:50:03Z</dc:date>
    </item>
    <item>
      <title>Re: Need to call a SQL query in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/391002#M93852</link>
      <description>&lt;P&gt;Awesome! Thanks for sharing your knowledge.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 19:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/391002#M93852</guid>
      <dc:creator>hamed_majidi_gmail_com</dc:creator>
      <dc:date>2017-08-25T19:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: Need to call a SQL query in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/391004#M93854</link>
      <description>&lt;P&gt;Thank you so much PGStats. I really appreciate your help and sharing of your knowledge.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 19:41:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-call-a-SQL-query-in-a-data-step/m-p/391004#M93854</guid>
      <dc:creator>hamed_majidi_gmail_com</dc:creator>
      <dc:date>2017-08-25T19:41:34Z</dc:date>
    </item>
  </channel>
</rss>

