<?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: combine data with string dates in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-combine-data-with-string-dates/m-p/760921#M80903</link>
    <description>&lt;P&gt;&lt;FONT face="georgia,palatino"&gt;Dear&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp; and All,&lt;BR /&gt;I have another question please.&lt;BR /&gt;How do I combine the demographic dataset that contains more patients to this sql please? So every patient from the demographic dataset will show in the final dataset regardless whether they are present in x or y dataset.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="georgia,palatino"&gt;Thanks a lot. Again!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 11 Aug 2021 14:36:08 GMT</pubDate>
    <dc:creator>Miracle</dc:creator>
    <dc:date>2021-08-11T14:36:08Z</dc:date>
    <item>
      <title>proc sql: combine data with string dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-combine-data-with-string-dates/m-p/759274#M80868</link>
      <description>&lt;P&gt;&lt;FONT face="georgia,palatino"&gt;Dear All,&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="georgia,palatino"&gt;Can I please seek your help on how to combine this 2 proc sql together?&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="georgia,palatino"&gt;The first proc sql is to obtaine the date of first and last treatment.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="georgia,palatino"&gt;The second proc sql is to identify treatment emergent AE.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="georgia,palatino"&gt;Instead of two separate proc sql, how can I achieve this by using one proc sql?&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="georgia,palatino"&gt;Thanks a lot.&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table txdat as 
	select id, 
	       min(input(ECSTD,yymmdd10.)) format=is8601da. length=8 as trtstd,
				 max(input(ECEND,yymmdd10.)) format=is8601da. length=8 as trtend
	from ec
	group by id;
quit;
proc sql;
	create table chk as 
	select coalesce (x.id,y.id) as id, 
				 x.trtstd, 
				 y.AEYN,
				 y.aeterm, 
				 case when AESTDT &amp;lt;&amp;gt; "" then input(y.AESTDT,is8601da.) else . end as aestd format=is8601da. length=8,
				 case when AESTDT &amp;lt;&amp;gt; "" and trtstd &amp;lt;&amp;gt; . and calculated aestd &amp;gt;= trtstd then 1 else 0 end as teae,
				 y.tmp
	from txdat as x
	full join ae as y
	on x.id=y.id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Aug 2021 11:00:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-combine-data-with-string-dates/m-p/759274#M80868</guid>
      <dc:creator>Miracle</dc:creator>
      <dc:date>2021-08-04T11:00:06Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql: combine data with string dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-combine-data-with-string-dates/m-p/759278#M80869</link>
      <description>&lt;P&gt;There might be more performant ways by re-formulating your SQL query but one quick way is to just copy/paste your 1st SQL to your 2nd SQL. This is basically replacing the table used in your 2nd SQL (created in your first SQL) with an inline-view.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table chk as 
	select coalesce (x.id,y.id) as id, 
				 x.trtstd, 
				 y.AEYN,
				 y.aeterm, 
				 case when AESTDT &amp;lt;&amp;gt; "" then input(y.AESTDT,is8601da.) else . end as aestd format=is8601da. length=8,
				 case when AESTDT &amp;lt;&amp;gt; "" and trtstd &amp;lt;&amp;gt; . and calculated aestd &amp;gt;= trtstd then 1 else 0 end as teae,
				 y.tmp
	from 
    (
    	select id, 
    	       min(input(ECSTD,yymmdd10.)) format=is8601da. length=8 as trtstd,
    				 max(input(ECEND,yymmdd10.)) format=is8601da. length=8 as trtend
    	from ec
    	group by id
    ) as x
	full join ae as y
	on x.id=y.id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another option which I prefer if all the data is within SAS (not connecting to a database) is to just create a view with the first SQL. This is from a performance perspective the same like using an inline view but it's easier to debug and also to read the code imho. So here the only change to your code would be a create View instead of a create Table in your first SQL.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create view txdat as 
	select id, 
	       min(input(ECSTD,yymmdd10.)) format=is8601da. length=8 as trtstd,
				 max(input(ECEND,yymmdd10.)) format=is8601da. length=8 as trtend
	from ec
	group by id;
quit;
proc sql;
	create table chk as 
	select coalesce (x.id,y.id) as id, 
				 x.trtstd, 
				 y.AEYN,
				 y.aeterm, 
				 case when AESTDT &amp;lt;&amp;gt; "" then input(y.AESTDT,is8601da.) else . end as aestd format=is8601da. length=8,
				 case when AESTDT &amp;lt;&amp;gt; "" and trtstd &amp;lt;&amp;gt; . and calculated aestd &amp;gt;= trtstd then 1 else 0 end as teae,
				 y.tmp
	from txdat as x
	full join ae as y
	on x.id=y.id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And last but not least: If going for an approach with two SQL's with the first one being a view it's often helpful during development or for debugging to create a table in the first SQL. But then when changing the table to a view in the same SAS session SAS will throw an error as it can't replace a view with a table (or vice versa). To overcome this obstacle I often add the following code on top of the SQLs:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc datasets lib=work memtype=(data view) nolist nowarn;
  delete txtdat;
  run;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Because of using option NOWARN this proc datasets won't throw a warning if there isn't any table or view with the name txtdat so all good also when running the code the first time in a new SAS session.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Aug 2021 11:19:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-combine-data-with-string-dates/m-p/759278#M80869</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-08-04T11:19:56Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql: combine data with string dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-combine-data-with-string-dates/m-p/759702#M80875</link>
      <description>&lt;P&gt;&lt;FONT face="georgia,palatino" size="4"&gt;Thanks a lot&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="georgia,palatino" size="4"&gt; It's a very detailed explanation for someone who isn't a sql person.&lt;BR /&gt;&lt;/FONT&gt;&lt;FONT face="georgia,palatino" size="4"&gt;I really appreciate it.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Aug 2021 14:27:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-combine-data-with-string-dates/m-p/759702#M80875</guid>
      <dc:creator>Miracle</dc:creator>
      <dc:date>2021-08-05T14:27:24Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql: combine data with string dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-combine-data-with-string-dates/m-p/760921#M80903</link>
      <description>&lt;P&gt;&lt;FONT face="georgia,palatino"&gt;Dear&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp; and All,&lt;BR /&gt;I have another question please.&lt;BR /&gt;How do I combine the demographic dataset that contains more patients to this sql please? So every patient from the demographic dataset will show in the final dataset regardless whether they are present in x or y dataset.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="georgia,palatino"&gt;Thanks a lot. Again!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Aug 2021 14:36:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-combine-data-with-string-dates/m-p/760921#M80903</guid>
      <dc:creator>Miracle</dc:creator>
      <dc:date>2021-08-11T14:36:08Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql: combine data with string dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-combine-data-with-string-dates/m-p/761043#M80904</link>
      <description>&lt;P&gt;You can add a left join to the SQL and then use coalesce() in the Select for any column that could come from more than one table.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Aug 2021 00:29:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-combine-data-with-string-dates/m-p/761043#M80904</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-08-12T00:29:02Z</dc:date>
    </item>
  </channel>
</rss>

