<?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 in my sql code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-in-my-sql-code/m-p/460402#M117017</link>
    <description>&lt;P&gt;Dear,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some one already helped me in my code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Link&amp;nbsp;&amp;nbsp;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Help-in-Proc-sql-code/m-p/460346#M117003" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/Help-in-Proc-sql-code/m-p/460346#M117003&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In order to get the output i need to do proc transpose after proc sql step. After the proc transpose step i had to do a data step to rename the variable name "NAME OF FORMER VARIABLE" to "Param" and I had to delete col 2 to col 4. Is there anything i can do to modify my sql code to get the output i need without additional transpose and data step. Thank you&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id trt$ date $18.;
DATALINES;
1 A 2014-04-26T13:10
1 A 2014-05-17T11:30
1 A 2014-06-07T11:45
1 A 2014-06-28T13:30
1 B 2014-04-26T13:10
1 B 2014-05-17T11:30
1 B 2014-06-07T11:45
1 B 2014-06-28T13:30
2 A 2014-04-26T13:10
2 A 2014-05-17T11:30
2 A 2014-06-07T11:45
2 A 2014-06-28T13:30
2 B 2014-04-26T13:10
2 B 2014-05-17T11:30
2 B 2014-06-07T11:45
2 B 2014-06-28T13:30
;
PROC SQL;
  CREATE TABLE TWO AS
    SELECT id,trt,
           input(date,is8601dt.) as exstdt format datetime18.,
           count(id) as numberofrecords,
           intck('week',min(datepart(calculated exstdt)),
              max(datepart(calculated exstdt)),'c') as durationinweeks,
           calculated numberofrecords/(round(calculated durationinweeks/3)+1) as compliance
      from one
        group by id,trt         
  ;
QUIT;

proc transpose data=two out=three;
by id trt;
var numberofrecords durationinweeks compliance;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;output needed:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;id&amp;nbsp; &amp;nbsp; &amp;nbsp; trt&amp;nbsp; &amp;nbsp; &amp;nbsp; param&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; aval&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; numberofrecords&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;A&amp;nbsp; &amp;nbsp; &amp;nbsp; durartioninweeks&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/SPAN&gt;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; compliance&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; numberofrecords&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; durartioninweeks&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/SPAN&gt;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;compliance&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;numberofrecords&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;durartioninweeks&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 9&lt;/SPAN&gt;&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;compliance&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; numberofrecords&amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;durartioninweeks&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;compliance&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 07 May 2018 12:23:48 GMT</pubDate>
    <dc:creator>knveraraju91</dc:creator>
    <dc:date>2018-05-07T12:23:48Z</dc:date>
    <item>
      <title>Help in my sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-in-my-sql-code/m-p/460402#M117017</link>
      <description>&lt;P&gt;Dear,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some one already helped me in my code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Link&amp;nbsp;&amp;nbsp;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Help-in-Proc-sql-code/m-p/460346#M117003" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/Help-in-Proc-sql-code/m-p/460346#M117003&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In order to get the output i need to do proc transpose after proc sql step. After the proc transpose step i had to do a data step to rename the variable name "NAME OF FORMER VARIABLE" to "Param" and I had to delete col 2 to col 4. Is there anything i can do to modify my sql code to get the output i need without additional transpose and data step. Thank you&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id trt$ date $18.;
DATALINES;
1 A 2014-04-26T13:10
1 A 2014-05-17T11:30
1 A 2014-06-07T11:45
1 A 2014-06-28T13:30
1 B 2014-04-26T13:10
1 B 2014-05-17T11:30
1 B 2014-06-07T11:45
1 B 2014-06-28T13:30
2 A 2014-04-26T13:10
2 A 2014-05-17T11:30
2 A 2014-06-07T11:45
2 A 2014-06-28T13:30
2 B 2014-04-26T13:10
2 B 2014-05-17T11:30
2 B 2014-06-07T11:45
2 B 2014-06-28T13:30
;
PROC SQL;
  CREATE TABLE TWO AS
    SELECT id,trt,
           input(date,is8601dt.) as exstdt format datetime18.,
           count(id) as numberofrecords,
           intck('week',min(datepart(calculated exstdt)),
              max(datepart(calculated exstdt)),'c') as durationinweeks,
           calculated numberofrecords/(round(calculated durationinweeks/3)+1) as compliance
      from one
        group by id,trt         
  ;
QUIT;

proc transpose data=two out=three;
by id trt;
var numberofrecords durationinweeks compliance;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;output needed:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;id&amp;nbsp; &amp;nbsp; &amp;nbsp; trt&amp;nbsp; &amp;nbsp; &amp;nbsp; param&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; aval&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; numberofrecords&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;A&amp;nbsp; &amp;nbsp; &amp;nbsp; durartioninweeks&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/SPAN&gt;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; compliance&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; numberofrecords&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; durartioninweeks&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/SPAN&gt;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;compliance&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;numberofrecords&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;durartioninweeks&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 9&lt;/SPAN&gt;&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;compliance&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; numberofrecords&amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;durartioninweeks&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;compliance&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 May 2018 12:23:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-in-my-sql-code/m-p/460402#M117017</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2018-05-07T12:23:48Z</dc:date>
    </item>
    <item>
      <title>Re: Help in my sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-in-my-sql-code/m-p/460404#M117019</link>
      <description>&lt;P&gt;You can add rename and drop options to the output dataset of your proc transpose:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=two out=three&lt;STRONG&gt;(rename=(_NAME_=param col1=aval) drop=col2-col4)&lt;/STRONG&gt;;
	by id trt;
	var numberofrecords durationinweeks compliance;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As far as coding transpose in SQL: I wish anyone good luck. Many have tried and most (all?) have failed. You could consider a datastep but that would not necessarily improve your program as a whole. But it can be done.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;- Jan.&lt;/P&gt;</description>
      <pubDate>Mon, 07 May 2018 12:49:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-in-my-sql-code/m-p/460404#M117019</guid>
      <dc:creator>jklaverstijn</dc:creator>
      <dc:date>2018-05-07T12:49:19Z</dc:date>
    </item>
    <item>
      <title>Re: Help in my sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-in-my-sql-code/m-p/460408#M117020</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't think its a bad idea to transpose as you want , but keeping In mind that you are deriving the required data in sql step, it would need additional step to get the required format i.e. transpose . here you can eliminate data step by using options.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;transpose&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;data&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=two &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;out&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=three(&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;rename&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=(_name_=param col1=aval) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;drop&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;= col2 col3 col4);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; id trt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;var&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; numberofrecords durationinweeks compliance;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Thanks,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Vish&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 07 May 2018 13:05:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-in-my-sql-code/m-p/460408#M117020</guid>
      <dc:creator>Vish33</dc:creator>
      <dc:date>2018-05-07T13:05:00Z</dc:date>
    </item>
    <item>
      <title>Re: Help in my sql code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-in-my-sql-code/m-p/463698#M118178</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;By "to get the output i need without additional transpose and data step", I assumed you expected to get the output within two steps of Data Step and/or PROC SQL. Basically, to achieve the result&amp;nbsp;similar to that&amp;nbsp;of PROC TRANSPOSE, the statement UNION is recommended in PROC SQL. Please see the example program below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
   input id trt $ date ymddttm16.;
   format date datetime18.;
   datalines;
1 A 2014-04-26T13:10
1 A 2014-05-17T11:30
1 A 2014-06-07T11:45
1 A 2014-06-28T13:30
1 B 2014-04-26T13:10
1 B 2014-05-17T11:30
1 B 2014-06-07T11:45
1 B 2014-06-28T13:30
2 A 2014-04-26T13:10
2 A 2014-05-17T11:30
2 A 2014-06-07T11:45
2 A 2014-06-28T13:30
2 B 2014-04-26T13:10
2 B 2014-05-17T11:30
2 B 2014-06-07T11:45
2 B 2014-06-28T13:30
   ;
run;

proc sql;
   create table two as
   select distinct id, trt, 'numberofrecords' as param length=32, 1 as paramn,
      count(*) as aval
   from one
   group by id, trt
   union
   select distinct id, trt, 'durationinweeks', 2,
      intck('week', min(datepart(date)), max(datepart(date)), 'c')
   from one
   group by id, trt
   select distinct id, trt, 'compliance', 3,
      count(*)/(round(intck('week', min(datepart(date)), max(datepart(date)), 'c')/3)+1)
   from one
   group by id, trt
   order by id, trt, paramn;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;There is, of course, an obvious downside in this two-step programming, which is redundancy, e.g. the&amp;nbsp;GROUP BY statement; and the&amp;nbsp;CALCULATED statement would be no longer applicable since "number of records" and "duration in weeks" were not defined in the same SELECT statement any more. In addition, if the definition of variable PARAMN&amp;nbsp;was&amp;nbsp;skipped, observations in data set TWO would be sorted by ID, TRT, and PARAM alphabetically.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To avoid such redundancy, I'm afraid a third step of PROC TRANSPOSE is necessary. You can apply the NAME= option, along with the data set option RENAME= for the DATA= option&amp;nbsp;in PROC TRANSPOSE statement, something similar to the following, so that a fourth Data Step can be skipped:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose out=three( rename=(col1=aval)) name=param;
   var numberofrecords durationinweeks compliance;
   by id trt;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;To make sure COL2, COL3, and COL4&amp;nbsp;will not be&amp;nbsp;generated, it is essential to apply SELECT DISTINCT, instead of SELECT statement in PROC SQL.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;P.S. I took the liberty to modify the programming of Data Step ONE so that variable DATE is defined as numeric directly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this would Help!&lt;/P&gt;</description>
      <pubDate>Mon, 21 May 2018 10:54:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-in-my-sql-code/m-p/463698#M118178</guid>
      <dc:creator>jim_cai</dc:creator>
      <dc:date>2018-05-21T10:54:37Z</dc:date>
    </item>
  </channel>
</rss>

