<?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: Help in my Proc SQL code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-in-my-Proc-SQL-code/m-p/677870#M204539</link>
    <description>&lt;P&gt;Thank you very much for sql code. It gives me the output i need after adding &lt;FONT color="#FF0000"&gt;a.date&lt;/FONT&gt; in select statement. As i need to keep DATE variable&amp;nbsp; from dataset ONE.&lt;/P&gt;
&lt;P&gt;But in the data step code, i got the correct&amp;nbsp; FLAG variable values but I am not able to keep same DATE variable values from dataset ONE. After keeping DATE variable in Keep statement, I am getting DATE values from dataset&amp;nbsp; TWO&amp;nbsp; for id=2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;code worked&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;proc sql;
  create table threeb as
  select a.id, a.date
    case when b.date&amp;gt;a.date then 'Y'
	     else 'N'
	end as flag

	from one as a
	left join two (where=(value='N')) as b

	on a.id=b.id
	group by a.id
	having b.date=max(b.date) ;
quit;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 19 Aug 2020 17:03:06 GMT</pubDate>
    <dc:creator>knveraraju91</dc:creator>
    <dc:date>2020-08-19T17:03:06Z</dc:date>
    <item>
      <title>Help in my Proc SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-in-my-Proc-SQL-code/m-p/677814#M204515</link>
      <description>&lt;P&gt;Dear,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to create a variable in data set 'ONE' by comparing &lt;FONT color="#FF0000"&gt;DATE&lt;/FONT&gt; variable from&amp;nbsp;dataset 'ONE'&amp;nbsp; and &lt;FONT color="#FF0000"&gt;DATE and VALUE&lt;/FONT&gt; variables from&amp;nbsp; data set 'TWO' .&lt;/P&gt;
&lt;P&gt;If there is a record in dataset TWO&amp;nbsp; with VALUE='N' and&amp;nbsp; DATE greater than DATE&amp;nbsp; in&amp;nbsp; dataset ONE,&amp;nbsp; then i need to create FLAG='Y'.&amp;nbsp; If no such record then FLAG is "N"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Only for id=2,&amp;nbsp; there is a record in data set TWO with DATE greater than date in ONE&amp;nbsp; dataset&amp;nbsp; &amp;nbsp;and with value ="N'.&amp;nbsp; Hence only ID=2 populated with flag='Y'.&amp;nbsp; &amp;nbsp; Please suggest in my code. I am getting all possible records. Thank you&lt;/P&gt;
&lt;P&gt;output need;&lt;/P&gt;
&lt;P&gt;id&amp;nbsp; &amp;nbsp;date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; flag&lt;/P&gt;
&lt;P&gt;1 2019-01-18&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;N&lt;BR /&gt;2 2019-02-12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Y&lt;BR /&gt;3 2019-03-15&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;N&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id date yymmdd10.;
format date yymmdd10.;
datalines;
1 2019-01-18
2 2019-02-12
3 2019-03-15
;
data two;
input id date yymmdd10. value $;
format date yymmdd10.;
datalines;
1 2019-01-01 N
1 2019-01-11 Y
1 2019-01-18 N
2 2019-02-02 N
2 2019-02-22 N
3 2019-03-05 Y
3 2019-03-25 Y
;
proc sql;
create table three as
select *,
case
when a.date lt b.date and b.value='N' then 'Y'
else 'N' end as flag
from one as a left join two as b
on a.id=b.id
order by a.id,a.date;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Aug 2020 14:44:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-in-my-Proc-SQL-code/m-p/677814#M204515</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2020-08-19T14:44:41Z</dc:date>
    </item>
    <item>
      <title>Re: Help in my Proc SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-in-my-Proc-SQL-code/m-p/677821#M204518</link>
      <description>&lt;P&gt;Are you trying to produce one record per ID with flag representing the presence of one qualifying obs from dataset two?&amp;nbsp; Or do you want the traditional "one left join two" results, with one record per qualifying join?&lt;/P&gt;</description>
      <pubDate>Wed, 19 Aug 2020 15:02:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-in-my-Proc-SQL-code/m-p/677821#M204518</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-08-19T15:02:43Z</dc:date>
    </item>
    <item>
      <title>Re: Help in my Proc SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-in-my-Proc-SQL-code/m-p/677825#M204519</link>
      <description>Thank you very much. I need to output one record per output with flag representing the presence of one qualifying obs from dataset two&lt;BR /&gt; I need to keep only records from dataset one  and create a variable FLAG  with Y or N   depends on  data set  TWO has records with VALUE=N   and date greater than  date in date in ONE data set</description>
      <pubDate>Wed, 19 Aug 2020 15:16:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-in-my-Proc-SQL-code/m-p/677825#M204519</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2020-08-19T15:16:20Z</dc:date>
    </item>
    <item>
      <title>Re: Help in my Proc SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-in-my-Proc-SQL-code/m-p/677839#M204523</link>
      <description>&lt;P&gt;First, thank you for providing sample data in the form of a data step.&amp;nbsp; It's much easier to test proposed solutions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's mine:&amp;nbsp; &lt;STRONG&gt;&lt;EM&gt;(edited to require date from two to be greated, not just greater or equal to date from one)&lt;/EM&gt;&lt;/STRONG&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id date yymmdd10.;
format date yymmdd10.;
datalines;
1 2019-01-18
2 2019-02-12
3 2019-03-15
;
data two;
input id date yymmdd10. value $;
format date yymmdd10.;
datalines;
1 2019-01-01 N
1 2019-01-11 Y
1 2019-01-18 N
2 2019-02-02 N
2 2019-02-22 N
3 2019-03-05 Y
3 2019-03-25 Y
;
data three (keep=id flag);
  set one two (where=(value='N') in=intwo);
  by id date;
  if last.id;
  if intwo=1 and first.date=1 then flag='Y';  /*Added "first.date=1" to properly satisfy date requirement*/
  else flag='N';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Give the 2 datasets are sorted by ID/DATE, it's quite easy to produce what you want in a DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The SET&amp;nbsp; followed by BY statements tell SAS to interleave the obs from one and two, sorted by ID/DATE.&lt;/LI&gt;
&lt;LI&gt;The "where=" tells sas to process only obs from two that qualify for the value you specified.&lt;/LI&gt;
&lt;LI&gt;The intwo variable is a temporary dummy saying whether the (interleaved) record-in-hand is from two.&lt;/LI&gt;
&lt;LI&gt;The subsetting IF says to keep only the last record for each id.&lt;/LI&gt;
&lt;LI&gt;So if the last record is from TWO &lt;EM&gt;&lt;STRONG&gt;(and it is not a tied date wiht a record from ONE)&lt;/STRONG&gt;&lt;/EM&gt; , it therefore has a date that qualifies for your criterion.&amp;nbsp; And if not, then it doesn't, so calculate flag accordingly.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Wed, 19 Aug 2020 16:07:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-in-my-Proc-SQL-code/m-p/677839#M204523</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-08-19T16:07:08Z</dc:date>
    </item>
    <item>
      <title>Re: Help in my Proc SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-in-my-Proc-SQL-code/m-p/677846#M204527</link>
      <description>&lt;P&gt;An sql solution&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table threeb as
  select a.id, 
    case when b.date&amp;gt;a.date then 'Y'
	     else 'N'
	end as flag

	from one as a
	left join two (where=(value='N')) as b

	on a.id=b.id
	group by a.id
	having b.date=max(b.date) ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Aug 2020 16:11:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-in-my-Proc-SQL-code/m-p/677846#M204527</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-08-19T16:11:11Z</dc:date>
    </item>
    <item>
      <title>Re: Help in my Proc SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-in-my-Proc-SQL-code/m-p/677870#M204539</link>
      <description>&lt;P&gt;Thank you very much for sql code. It gives me the output i need after adding &lt;FONT color="#FF0000"&gt;a.date&lt;/FONT&gt; in select statement. As i need to keep DATE variable&amp;nbsp; from dataset ONE.&lt;/P&gt;
&lt;P&gt;But in the data step code, i got the correct&amp;nbsp; FLAG variable values but I am not able to keep same DATE variable values from dataset ONE. After keeping DATE variable in Keep statement, I am getting DATE values from dataset&amp;nbsp; TWO&amp;nbsp; for id=2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;code worked&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;proc sql;
  create table threeb as
  select a.id, a.date
    case when b.date&amp;gt;a.date then 'Y'
	     else 'N'
	end as flag

	from one as a
	left join two (where=(value='N')) as b

	on a.id=b.id
	group by a.id
	having b.date=max(b.date) ;
quit;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Aug 2020 17:03:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-in-my-Proc-SQL-code/m-p/677870#M204539</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2020-08-19T17:03:06Z</dc:date>
    </item>
    <item>
      <title>Re: Help in my Proc SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-in-my-Proc-SQL-code/m-p/677871#M204540</link>
      <description>&lt;P&gt;Translate your requirement directly as a correlated subquery:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table three as
select 
    id,
    date,
    case when exists (
        select * from two where id=a.id and value="N" and date &amp;gt; a.date) 
        then "Y" else "N" end as flag
from one as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Aug 2020 17:05:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-in-my-Proc-SQL-code/m-p/677871#M204540</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-08-19T17:05:12Z</dc:date>
    </item>
  </channel>
</rss>

