<?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: Converting Datastep to SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695433#M212221</link>
    <description>Thank you Andreas,&lt;BR /&gt;&lt;BR /&gt;I have searched the concept of By-group processing in SQL and it seems its non-existence that''s why i have to post this question.&lt;BR /&gt;&lt;BR /&gt;I will continue using the datastep then.</description>
    <pubDate>Fri, 30 Oct 2020 10:47:07 GMT</pubDate>
    <dc:creator>queenamaka</dc:creator>
    <dc:date>2020-10-30T10:47:07Z</dc:date>
    <item>
      <title>Converting Datastep to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695423#M212217</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;please can this programme below be converted to Proc SQL&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data A;&lt;/P&gt;&lt;P&gt;Set B;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;retain EXPC1FL EXPC2FL EXPC3FL TRTPFSDT TRTPLSDT EXPDOSE EXPCYCN;&lt;BR /&gt;format EXPC1FL EXPC2FL EXPC3FL $1. TRTPFSDT TRTPLSDT DATE9. EXPDUR EXPCYCN EXPDOSE 8. EXPCOMP 4.1;&lt;BR /&gt;by usubjid visitnum;&lt;BR /&gt;if first.usubjid then do;&lt;BR /&gt;EXPC1FL=' '; EXPC2FL=' '; EXPC3FL=' ';&lt;BR /&gt;TRTPFSDT=.; TRTPLSDT=.; EXPDOSE=0; EXPCYCN=0;&lt;BR /&gt;end;&lt;BR /&gt;EXPDOSE=EXPDOSE+ECDOSE;&lt;BR /&gt;EXPCYCN=EXPCYCN+1;&lt;BR /&gt;if visitnum=10 then EXPC1FL='Y';&lt;BR /&gt;if visitnum=20 then EXPC2FL='Y';&lt;BR /&gt;if visitnum=30 then EXPC3FL='Y';&lt;BR /&gt;if visitnum=10 then TRTPFSDT=TRTPSDT;&lt;BR /&gt;if last.usubjid then TRTPLSDT=TRTPEDT;&lt;BR /&gt;if last.usubjid;&lt;BR /&gt;EXPCOMP=(EXPDOSE/250)*40;&lt;BR /&gt;EXPDUR=(TRTPLSDT - TRTPFSDT)+1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards&lt;/P&gt;&lt;P&gt;Queen&lt;/P&gt;</description>
      <pubDate>Fri, 30 Oct 2020 09:32:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695423#M212217</guid>
      <dc:creator>queenamaka</dc:creator>
      <dc:date>2020-10-30T09:32:51Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Datastep to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695427#M212219</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;it's possible, but it wouldn't be so readable and easy to validate as the program you already have. So I can't see why it would make sense right now.&lt;/P&gt;
&lt;P&gt;Could you please explain why you would like to do this?&lt;/P&gt;
&lt;P&gt;It would also be great if you could provide some data to play with.&lt;/P&gt;</description>
      <pubDate>Fri, 30 Oct 2020 10:26:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695427#M212219</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2020-10-30T10:26:53Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Datastep to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695428#M212220</link>
      <description>&lt;P&gt;Please use the "Insert SAS Code" button to insert formatted sas code.&lt;/P&gt;
&lt;P&gt;The concept of first/last observation in a group does not exist in sql.&lt;/P&gt;</description>
      <pubDate>Fri, 30 Oct 2020 10:27:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695428#M212220</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-10-30T10:27:10Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Datastep to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695433#M212221</link>
      <description>Thank you Andreas,&lt;BR /&gt;&lt;BR /&gt;I have searched the concept of By-group processing in SQL and it seems its non-existence that''s why i have to post this question.&lt;BR /&gt;&lt;BR /&gt;I will continue using the datastep then.</description>
      <pubDate>Fri, 30 Oct 2020 10:47:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695433#M212221</guid>
      <dc:creator>queenamaka</dc:creator>
      <dc:date>2020-10-30T10:47:07Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Datastep to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695436#M212222</link>
      <description>&lt;P&gt;SQL has the "GROUP BY" statement that allows to use aggregation functions on subgroups of the dataset.&lt;/P&gt;
&lt;P&gt;Here is a simplified exemple based on your data step (not tested as you did not provide sample data in&lt;/P&gt;
&lt;P&gt;the form of a datastep) :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
    CREATE TABLE want AS
    AS SELECT DISTINCT count(*) AS EXPCYCN, sum(ECDOSE) AS EXPDOSE,
              EXPC1FL=ifc(visitnum=10,'Y',' ') format=$1.
    FROM have
    GROUP BY usubjid
    ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;EDIT : Added "DISTINCT" to keep only one row by aggregate (assuming visitnum is constant for any given usubjid).&lt;/P&gt;</description>
      <pubDate>Fri, 30 Oct 2020 11:56:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695436#M212222</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-10-30T11:56:20Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Datastep to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695490#M212234</link>
      <description>&lt;P&gt;SQL and "sequence" of data do not play well.&lt;/P&gt;
&lt;P&gt;So anything that uses Retain statements or Lag and Dif functions is very difficult to translate to SQL. It also typically becomes much more convoluted code that may be a tad fragile.&lt;/P&gt;</description>
      <pubDate>Fri, 30 Oct 2020 14:34:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695490#M212234</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-30T14:34:01Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Datastep to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695593#M212276</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table A as
select
	usubjid,
	sum(ecdose) as expdose,
	count(*) as expcycn,
	calculated expdose / 250 * 40 as expcomp,
	case when visitnum=10 then "Y" else " " end as expc1fl,
	case when visitnum=20 then "Y" else " " end as expc2fl,
	case when visitnum=30 then "Y" else " " end as expc3fl,
    case when visitnum=10 then trtpsdt else . end as trtpfsdt,
	trtpedt as trtplsdt,
	calculated trtplsdt - calculated trtpfsdt + 1 as expdur
from B
group by usubjid
having visitnum=max(visitnum);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Oct 2020 21:23:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695593#M212276</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-10-30T21:23:38Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Datastep to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695605#M212281</link>
      <description>&lt;P&gt;This line seems strange to me.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; EXPC1FL=ifc(visitnum=10,'Y',' ') format=$1.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Why are trying to test if the existing variable EXPC1FL is equal to either the letter Y or a blank string?&amp;nbsp; Why didn't you give the newly calculated variable a name? Why did you try to attach a character format to a numeric (boolean) value?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also if you did mean to make a character variable why do you feel a need to attach the $1. format to limit the number of characters displayed when the variable is printed? Why not just set the length of the variable to 1?&lt;/P&gt;</description>
      <pubDate>Fri, 30 Oct 2020 23:22:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695605#M212281</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-10-30T23:22:16Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Datastep to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695609#M212282</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&lt;BR /&gt;You're right, it was a carelessness mistake. I meant "ifc(... ) AS EXPC1FL".&lt;BR /&gt;</description>
      <pubDate>Sat, 31 Oct 2020 01:11:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695609#M212282</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-10-31T01:11:16Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Datastep to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695611#M212283</link>
      <description>I also agree with your second point about the length though i think the result would be the same, but i cannot test it right now.</description>
      <pubDate>Sat, 31 Oct 2020 01:18:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695611#M212283</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-10-31T01:18:20Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Datastep to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695614#M212284</link>
      <description>&lt;P&gt;With that code neither is needed.&lt;/P&gt;
&lt;P&gt;But attaching $ formats to characters is a dangerous practice.&lt;/P&gt;
&lt;P&gt;Example: What output do you expect from this program?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one ;
  input study :$10. @@;
cards;
ABCD1234 ABCD1234B ABCD1234C
;
data two ;
   format study $8. ;
   input study @@;
cards;
WXYZ0001 WXYZ0002
;
data both;
  set one two;
run;
proc freq ;
 tables study;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;The FREQ Procedure

                                     Cumulative    Cumulative
study       Frequency     Percent     Frequency      Percent
-------------------------------------------------------------
ABCD1234           3       60.00             3        60.00
WXYZ0001           1       20.00             4        80.00
WXYZ0002           1       20.00             5       100.00&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;Note this is simplified version of a real problem that I have seen.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 31 Oct 2020 03:01:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695614#M212284</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-10-31T03:01:44Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Datastep to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695625#M212291</link>
      <description>Yes, i agree with you, unless truncation is a desired feature, applying a $ format to a character variable can be error prone.&lt;BR /&gt;My point was to show that columns attributes could be defined for new variables created by a proc sql and i used the format as in the original data step as an example.</description>
      <pubDate>Sat, 31 Oct 2020 08:19:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Datastep-to-SQL/m-p/695625#M212291</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-10-31T08:19:52Z</dc:date>
    </item>
  </channel>
</rss>

