<?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 Help need in Proc sql code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-need-in-Proc-sql-code/m-p/455812#M115365</link>
    <description>&lt;P&gt;Dear&lt;/P&gt;
&lt;P&gt;I am trying Proc sql step to reduce my the code in my program.&lt;/P&gt;
&lt;P&gt;I need to calculate baseline values for weight and height by comparing dates in ex with&amp;nbsp; vs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;output needed;&lt;/P&gt;
&lt;P&gt;id&amp;nbsp; &amp;nbsp;weightbl&amp;nbsp; heightbl&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 70&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 170&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The vdate should less than edate and i need to select the last obs close to on or before edate. Please help in my code. Thank you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vsone;
input id tcd $ vdate $10. value;
datalines;
1 weight 2016-04-06 69
1 weight 2016-04-18 70
1 weight 2016-04-19 .
1 weight 2016-05-18 80
1 height 2016-04-16 169
1 height 2016-04-18 170
;
data exone;
input id edate $19.;
datalines;
1 2016-04-26T13:10
;
proc sql;
create table want as
select *
from vone as a left join eone as b
on a.id=b.id and input(a.vdate, is8601da.)- datepart(input(b.edate, is8601dt.) =&amp;lt; 1
where value ne .
group by id 
having max(input(a.vdate,is8601da.)- datepart(input(b.edate,is8601dt.)));
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 20 Apr 2018 02:47:51 GMT</pubDate>
    <dc:creator>knveraraju91</dc:creator>
    <dc:date>2018-04-20T02:47:51Z</dc:date>
    <item>
      <title>Help need in Proc sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-need-in-Proc-sql-code/m-p/455812#M115365</link>
      <description>&lt;P&gt;Dear&lt;/P&gt;
&lt;P&gt;I am trying Proc sql step to reduce my the code in my program.&lt;/P&gt;
&lt;P&gt;I need to calculate baseline values for weight and height by comparing dates in ex with&amp;nbsp; vs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;output needed;&lt;/P&gt;
&lt;P&gt;id&amp;nbsp; &amp;nbsp;weightbl&amp;nbsp; heightbl&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 70&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 170&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The vdate should less than edate and i need to select the last obs close to on or before edate. Please help in my code. Thank you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vsone;
input id tcd $ vdate $10. value;
datalines;
1 weight 2016-04-06 69
1 weight 2016-04-18 70
1 weight 2016-04-19 .
1 weight 2016-05-18 80
1 height 2016-04-16 169
1 height 2016-04-18 170
;
data exone;
input id edate $19.;
datalines;
1 2016-04-26T13:10
;
proc sql;
create table want as
select *
from vone as a left join eone as b
on a.id=b.id and input(a.vdate, is8601da.)- datepart(input(b.edate, is8601dt.) =&amp;lt; 1
where value ne .
group by id 
having max(input(a.vdate,is8601da.)- datepart(input(b.edate,is8601dt.)));
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Apr 2018 02:47:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-need-in-Proc-sql-code/m-p/455812#M115365</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2018-04-20T02:47:51Z</dc:date>
    </item>
    <item>
      <title>Re: Help need in Proc sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-need-in-Proc-sql-code/m-p/455816#M115367</link>
      <description>&lt;P&gt;Your SQL is certainly not much shorter, and absolutely much more difficult to follow than a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;gt;&lt;SPAN&gt;&amp;nbsp;the last obs close to on or before edate&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;processing records in order screams data step.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data VSONE;
input ID TCD $ VDATE yymmdd10. VALUE;
datalines;
1 weight 2016-04-06 69
1 weight 2016-04-18 70
1 weight 2016-04-19 .
1 weight 2016-05-18 80
1 height 2016-04-16 169
1 height 2016-04-18 170
run;
data EXONE;
input ID EDATE yymmdd10.;
datalines;
1 2016-04-26T13:10
run;
data WANT;
  retain WEIGHTBL HEIGHTBL; 
  keep ID WEIGHTBL HEIGHTBL; 
  merge VSONE (rename=(VALUE=VALW VDATE=DATEW) where=(TCD='weight' &amp;amp; VALW))
        VSONE (rename=(VALUE=VALH VDATE=DATEH) where=(TCD='height' &amp;amp; VALH))
        EXONE ;
  by ID;  
  if first.ID then call missing( WEIGHTBL, HEIGHTBL);
  if DATEW&amp;lt;=EDATE then WEIGHTBL=VALW;
  if DATEH&amp;lt;=EDATE then HEIGHTBL=VALH;
  if last.ID then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;WEIGHTBL HEIGHTBL ID&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 70&amp;nbsp; &amp;nbsp; &amp;nbsp; 170&amp;nbsp; 1&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Apr 2018 03:59:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-need-in-Proc-sql-code/m-p/455816#M115367</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-04-20T03:59:23Z</dc:date>
    </item>
    <item>
      <title>Re: Help need in Proc sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-need-in-Proc-sql-code/m-p/455827#M115376</link>
      <description>&lt;P&gt;If you insist on SQL then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data VSONE;
input ID TCD $ VDATE yymmdd10. VALUE;
datalines;
1 weight 2016-04-06 69
1 weight 2016-04-18 70
1 weight 2016-04-19 .
1 weight 2016-05-18 80
1 height 2016-04-16 169
1 height 2016-04-18 170
;

data EXONE;
input ID EDATE yymmdd10.;
datalines;
1 2016-04-26T13:10
;

proc sql;
select 
    a.id, b.value as weight, c.value as height 
from 
    exone as a left join
    (select * from vsone where tcd="weight" and value is not missing) as b 
        on a.id=b.id and a.edate&amp;gt;= b.vdate left join
    (select * from vsone where tcd="height" and value is not missing) as c 
        on a.id=c.id and a.edate&amp;gt;= c.vdate
group by a.id
having b.vdate=max(b.vdate) and c.vdate=max(c.vdate);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Apr 2018 04:40:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-need-in-Proc-sql-code/m-p/455827#M115376</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-04-20T04:40:29Z</dc:date>
    </item>
    <item>
      <title>Re: Help need in Proc sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-need-in-Proc-sql-code/m-p/455853#M115389</link>
      <description>&lt;P&gt;Rather than hardcoding "height" and weight" I would use transpose to get the final table:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;proc sql;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; create table last as&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; select vsone.*&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; from vsone join exone&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; on vsone.id=exone.id and vsone.vdate &amp;lt;= exone.edate&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; where value ne .&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; group by vsone.id,tcd&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; having vsone.vdate=max(vsone.vdate);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;quit;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;proc transpose out=want(drop=_name_) suffix=tbl;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; by id;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; var value;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; id tcd;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I would have used the code window to post code, but for some reason "paste" does not work right now (just got a new Windows version, maybe therefore).&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Rather than convering all the date texts to SAS dates, you can use the fact that they are ordered (both formats start with yyyy-mm-dd). I dropped the aliases, they do save a few keystrokes, but otherwise they just make the program harder to read.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I also dropped the left join. But maybe you want the highest VDATE when there is no EDATE? Then use the left join again, and add "or exone.edate is null" to the where clause.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Apr 2018 08:59:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-need-in-Proc-sql-code/m-p/455853#M115389</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-04-20T08:59:58Z</dc:date>
    </item>
  </channel>
</rss>

