<?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 to do write sql code for data step code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-write-sql-code-for-data-step-code/m-p/669927#M201042</link>
    <description>&lt;P&gt;Personally I prefer a data step but here are two approaches.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=one; 
by id day;
run;

data temp;
set one (where=(flag='N'));
by ID;
impDay = day+1;
if last.ID;
keep id impday;
run;

data want;
merge one temp;
by id;
if missing(impday) then impday = 1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SQL option&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
create table want_sql as
select t1.*, coalesce(t2.max_day, 1) as impday
from one as t1
left join (select id, max(day)+1 as max_day from one where flag='N' group by ID) t2
on t1.id=t2.id;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 16 Jul 2020 17:01:02 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2020-07-16T17:01:02Z</dc:date>
    <item>
      <title>How to do write sql code for data step code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-write-sql-code-for-data-step-code/m-p/669924#M201039</link>
      <description>&lt;P&gt;Dear,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I got output i need but the pgm looks lengthy. Please suggest some alternative way or sql code. Thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to select for &lt;FONT color="#FF6600"&gt;day&lt;/FONT&gt; variable value and create &lt;FONT color="#FF6600"&gt;impday&lt;/FONT&gt;&amp;nbsp; for each&lt;FONT color="#FF6600"&gt; id by selecting&amp;nbsp; first obs after last obs where flag='N'.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;for id=1,&amp;nbsp; the first obs after last obs where flag='N' is&amp;nbsp; &amp;nbsp;day=5. so IMPDAY=5&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;for id=2, the first obs after last obs where flag='N' is&amp;nbsp; &amp;nbsp;day=5. so IMPDAY=5&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;for id=3, there is no obs where flag='N' so i need to take first obs&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;for id=4&amp;nbsp; there is one record so i select&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;output needed&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;id flag day&lt;FONT color="#FF6600"&gt; impday&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;1 Y 1 5&lt;BR /&gt;1 Y 2 5&lt;BR /&gt;1 Y 3 5&lt;BR /&gt;1 N 4 5&lt;BR /&gt;1 Y 5 5&lt;BR /&gt;1 Y 6 5&lt;BR /&gt;2 N 1 5&lt;BR /&gt;2 Y 2 5&lt;BR /&gt;2 Y 3 5&lt;BR /&gt;2 N 4 5&lt;BR /&gt;2 Y 5 5&lt;BR /&gt;2 Y 6 5&lt;BR /&gt;3 Y 1 1&lt;BR /&gt;3 Y 2 1&lt;BR /&gt;3 Y 3 1&lt;BR /&gt;4 Y 1 1&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id flag$ day;
datalines;
1 Y 1
1 Y 2
1 Y 3
1 N 4
1 Y 5
1 Y 6
2 N 1
2 Y 2
2 Y 3
2 N 4
2 Y 5
2 Y 6
3 Y 1
3 Y 2
3 Y 3
4 Y 1
;

proc sort data=one;
by id descending flag;
run;

data two;
set one;
by id descending flag;
if last.id;
p_flag=flag;
p_day=day;
drop flag day;
run;

data three;
merge one(in=a) two(In=b);
by id;
if a;
if p_flag='N' and day lt p_day then delete;
run;

proc sort data=three;
by id flag;
run;

data four;
set three;
by id flag;
if flag='N' and first.id ^=last.id then delete;
run;

proc sort data=four;
by id flag;
run;
data five;
set four;
by id flag;
if first.id;
impday=day;
keep id impday;
run;

data six;
merge one(in=a) five;
by id;
if a;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Jul 2020 16:27:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-write-sql-code-for-data-step-code/m-p/669924#M201039</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2020-07-16T16:27:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to do write sql code for data step code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-write-sql-code-for-data-step-code/m-p/669925#M201040</link>
      <description>&lt;P&gt;Since Proc SQL does not really have a good sense of "order" of records your "first obs after Last obs flag='N' " it is unlikely to be a good tool alone.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What do you want when the last observation for an ID has flag="N"? The next observation is from a different ID.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jul 2020 16:45:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-write-sql-code-for-data-step-code/m-p/669925#M201040</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-07-16T16:45:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to do write sql code for data step code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-write-sql-code-for-data-step-code/m-p/669926#M201041</link>
      <description>Thank you very much for support. i set to missing if id has only one record and has "N'</description>
      <pubDate>Thu, 16 Jul 2020 16:53:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-write-sql-code-for-data-step-code/m-p/669926#M201041</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2020-07-16T16:53:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to do write sql code for data step code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-write-sql-code-for-data-step-code/m-p/669927#M201042</link>
      <description>&lt;P&gt;Personally I prefer a data step but here are two approaches.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=one; 
by id day;
run;

data temp;
set one (where=(flag='N'));
by ID;
impDay = day+1;
if last.ID;
keep id impday;
run;

data want;
merge one temp;
by id;
if missing(impday) then impday = 1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SQL option&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
create table want_sql as
select t1.*, coalesce(t2.max_day, 1) as impday
from one as t1
left join (select id, max(day)+1 as max_day from one where flag='N' group by ID) t2
on t1.id=t2.id;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Jul 2020 17:01:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-write-sql-code-for-data-step-code/m-p/669927#M201042</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-16T17:01:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to do write sql code for data step code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-write-sql-code-for-data-step-code/m-p/669961#M201061</link>
      <description>&lt;P&gt;Thank you very much for support. The code is working for some OBS only if &lt;FONT color="#FF6600"&gt;DAY&lt;/FONT&gt; values are sequential order. But if day variable values are not sequential order impday= DAY+1 is not giving me correct output as mentioned in code. I need to take next OBS after last obs having flag='N'. Please suggest.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jul 2020 19:15:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-write-sql-code-for-data-step-code/m-p/669961#M201061</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2020-07-16T19:15:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to do write sql code for data step code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-write-sql-code-for-data-step-code/m-p/669965#M201064</link>
      <description>Please provide sample data that reflects your business requirements.</description>
      <pubDate>Thu, 16 Jul 2020 19:38:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-write-sql-code-for-data-step-code/m-p/669965#M201064</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-16T19:38:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to do write sql code for data step code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-write-sql-code-for-data-step-code/m-p/669974#M201068</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/68272"&gt;@knveraraju91&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you very much for support. The code is working for some OBS only if &lt;FONT color="#FF6600"&gt;DAY&lt;/FONT&gt; values are sequential order. But if day variable values are not sequential order impday= DAY+1 is not giving me correct output as mentioned in code. I need to take next OBS after last obs having flag='N'. Please suggest.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Since all of your example data shows DAY as sequential you implied that was the case.&lt;/P&gt;
&lt;P&gt;If you do not provide representative data it is hard to generate a general solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And per my previous question what if the last value when you have more than one value in an id group that the flag is 'N'. You only answered for a specific case of exactly one value.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jul 2020 20:10:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-write-sql-code-for-data-step-code/m-p/669974#M201068</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-07-16T20:10:31Z</dc:date>
    </item>
  </channel>
</rss>

