<?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 help in my code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-help-in-my-code/m-p/456756#M115734</link>
    <description>&lt;P&gt;Here is a slightly modified version to account for duplicate dates:&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 $ VALUE VDATE :yymmdd10.;
format vdate yymmdd10.;
datalines;
1 WEIGHT 20 2015-02-09
1 WEIGHT 30 2015-02-19
2 WEIGHT 40 2015-03-09
2 WEIGHT . 2015-03-10
2 WEIGHT 60 2015-03-11
3 WEIGHT 70 2015-04-08
3 WEIGHT 80 2015-04-09
3 WEIGHT 90 2015-04-09
4 WEIGHT 90 2015-04-08
4 WEIGHT 80 2015-04-09
4 WEIGHT 70 2015-04-09
1 HEIGHT 120 2015-02-09
1 HEIGHT 130 2015-02-19
2 HEIGHT 140 2015-03-09
2 HEIGHT . 2015-03-10
2 HEIGHT 160 2015-03-10
3 HEIGHT 170 2015-04-08
3 HEIGHT 180 2015-04-09
3 HEIGHT 190 2015-04-09
4 HEIGHT 190 2015-04-08
4 HEIGHT 180 2015-04-09
4 HEIGHT 170 2015-04-09
;

data EXONE;
input ID EDATE :yymmdd10.;
format edate yymmdd10.;
datalines;
1 2015-02-10
2 2015-03-10
3 2015-04-10
4 2015-04-10
;

proc sql;
select 
    a.id, b.value as weight, c.value as height 
from 
    exone as a left join
    (select id, vdate, max(value) as value 
     from vsone 
     where tcd="WEIGHT" and value is not missing
     group by id, vdate) as b 
        on a.id=b.id and a.edate&amp;gt;= b.vdate left join
    (select id, vdate, max(value) as value 
     from vsone 
     where tcd="HEIGHT" and value is not missing
     group by id, vdate) 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>Tue, 24 Apr 2018 03:35:17 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2018-04-24T03:35:17Z</dc:date>
    <item>
      <title>Proc sql help in my code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-help-in-my-code/m-p/456639#M115681</link>
      <description>&lt;P&gt;Dear ,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some one helped me yesterday. But for usubjid='3' , I need to write one conditional statement in the 'Having' statement to get max value when dates are equal. I included "b.vsstresn=max(b.vsstresn) and c.vsstresn=max(c.vsstresn).&lt;/P&gt;
&lt;P&gt;I got the output I need for usubjid='3', but the usubjid ='4' is not seen in the output. Please suggest. Thank you&lt;/P&gt;
&lt;P&gt;output need;&lt;/P&gt;
&lt;P&gt;usubjid&amp;nbsp; &amp;nbsp; weightbl&amp;nbsp; &amp;nbsp; heightbl&amp;nbsp; &amp;nbsp; bmibl&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;120&amp;nbsp; &amp;nbsp; &amp;nbsp; 13.8&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 160&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15.6&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;90&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 190&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;24.3&lt;/P&gt;
&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 90&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;190&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 24.3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data __ex1;
input usubjid exstdtc $10.;
datalines;
1 2015-02-10
2 2015-03-10
3 2015-04-10
4 2015-04-10
;
data __ex1;
set __ex1;
exstdtcn=input(exstdtc,is8601da.);
format exstdtcn date9.;
run;
data __vs;
input usubjid vstestcd $ vsstresn vsdtc $10.;
datalines;
1 WEIGHT 20 2015-02-09
1 WEIGHT 30 2015-02-19
2 WEIGHT 40 2015-03-09
2 WEIGHT . 2015-03-10
2 WEIGHT 60 2015-03-11
3 WEIGHT 70 2015-04-08
3 WEIGHT 80 2015-04-09
3 WEIGHT 90 2015-04-09
4 WEIGHT 90 2015-04-08
4 WEIGHT 80 2015-04-09
4 WEIGHT 70 2015-04-09
1 HEIGHT 120 2015-02-09
1 HEIGHT 130 2015-02-19
2 HEIGHT 140 2015-03-09
2 HEIGHT . 2015-03-10
2 HEIGHT 160 2015-03-10
3 HEIGHT 170 2015-04-08
3 HEIGHT 180 2015-04-09
3 HEIGHT 190 2015-04-09
4 HEIGHT 190 2015-04-08
4 HEIGHT 180 2015-04-09
4 HEIGHT 170 2015-04-09
;
DATA __VS;
set __vs;
vsdtcn=input(vsdtc,is8601da.);
format vsdtcn date9.;
run;

proc sql;
create table __basevalwh as
select *,(WEIGHTBL/HEIGHTBL/HEIGHTBL)*10000 as BMIBL from (select a.usubjid,b.vsstresn as weightbl,c.vsstresn as heightbl,b.vsdtc as avs,c.vsdtc as cvs
from 
    __ex1 as a left join
    (select * from __vs where vstestcd="WEIGHT" and vsstresn is not missing) as b 
        on a.usubjid=b.usubjid and a.exstdtcn&amp;gt;= b.vsdtcn left join 
		(select * from __vs where vstestcd="HEIGHT" and vsstresn is not missing) as c 
        on a.usubjid=c.usubjid and a.exstdtcn &amp;gt;= c.vsdtcn
	group by a.usubjid
having b.vsdtcn=max(b.vsdtcn) and c.vsdtcn=max(c.vsdtcn) and b.vsstresn=max(b.vsstresn) and c.vsstresn=max(c.vsstresn))
 ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Apr 2018 18:48:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-help-in-my-code/m-p/456639#M115681</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2018-04-23T18:48:14Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql help in my code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-help-in-my-code/m-p/456665#M115691</link>
      <description>&lt;P&gt;How can you expect that output with the condition that won't let.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;b.vsdtcn=max(b.vsdtcn) &lt;/STRONG&gt;this will exclude the records that have&amp;nbsp;vsstresn=90 which is MAX value for group 4,hence you will not get in output with the condition you have.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select a.*,b.*,c.* ,max(b.vsstresn) as Max_vsstresn,max(c.vsdtcn) format=date9.  as max_vsdtcn,max(b.vsdtcn) format=date9. as max_vsdtcn

from __ex1 as a
left join __vs as b
on a.usubjid=b.usubjid and a.exstdtcn&amp;gt;= b.vsdtcn
left join __vs as c
on a.usubjid=c.usubjid and a.exstdtcn &amp;gt;= c.vsdtcn 
where  b.vstestcd="WEIGHT" and b.vsstresn is not missing and c.vstestcd="HEIGHT" and c.vsstresn is not missing
group by b.usubjid
/* having b.vsdtcn=max(b.vsdtcn) and c.vsdtcn=max(c.vsdtcn) and b.vsstresn=max(b.vsstresn) */
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/20066i59DAA8A3022919AE/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Apr 2018 19:42:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-help-in-my-code/m-p/456665#M115691</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-04-23T19:42:52Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql help in my code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-help-in-my-code/m-p/456756#M115734</link>
      <description>&lt;P&gt;Here is a slightly modified version to account for duplicate dates:&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 $ VALUE VDATE :yymmdd10.;
format vdate yymmdd10.;
datalines;
1 WEIGHT 20 2015-02-09
1 WEIGHT 30 2015-02-19
2 WEIGHT 40 2015-03-09
2 WEIGHT . 2015-03-10
2 WEIGHT 60 2015-03-11
3 WEIGHT 70 2015-04-08
3 WEIGHT 80 2015-04-09
3 WEIGHT 90 2015-04-09
4 WEIGHT 90 2015-04-08
4 WEIGHT 80 2015-04-09
4 WEIGHT 70 2015-04-09
1 HEIGHT 120 2015-02-09
1 HEIGHT 130 2015-02-19
2 HEIGHT 140 2015-03-09
2 HEIGHT . 2015-03-10
2 HEIGHT 160 2015-03-10
3 HEIGHT 170 2015-04-08
3 HEIGHT 180 2015-04-09
3 HEIGHT 190 2015-04-09
4 HEIGHT 190 2015-04-08
4 HEIGHT 180 2015-04-09
4 HEIGHT 170 2015-04-09
;

data EXONE;
input ID EDATE :yymmdd10.;
format edate yymmdd10.;
datalines;
1 2015-02-10
2 2015-03-10
3 2015-04-10
4 2015-04-10
;

proc sql;
select 
    a.id, b.value as weight, c.value as height 
from 
    exone as a left join
    (select id, vdate, max(value) as value 
     from vsone 
     where tcd="WEIGHT" and value is not missing
     group by id, vdate) as b 
        on a.id=b.id and a.edate&amp;gt;= b.vdate left join
    (select id, vdate, max(value) as value 
     from vsone 
     where tcd="HEIGHT" and value is not missing
     group by id, vdate) 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>Tue, 24 Apr 2018 03:35:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-help-in-my-code/m-p/456756#M115734</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-04-24T03:35:17Z</dc:date>
    </item>
  </channel>
</rss>

