<?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 How to write complex queries in data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-complex-queries-in-data-step/m-p/512463#M138037</link>
    <description>&lt;P&gt;Dear Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to write some code from proc sql to data step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this is my proc sql code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table Risk_Register as
select distinct risk_id lable 'Risk ID',&lt;BR /&gt; RISK_NM lable = 'Risk Title',&lt;BR /&gt; RISK_DESC lable 'Risk Description',
 ctrl.control_inst_desc lable 'Control Description', &lt;BR /&gt; cf1.value_cd as sample1 label 'sample1&amp;nbsp;',
 cf2.value_cd as sample2&amp;nbsp;label 'sample2',&lt;BR /&gt; cf3.value_cd as sample3&amp;nbsp;label 'sample3',
 u.user_id lable 'User ID',&lt;BR /&gt; u.display_nm lable 'User Name'
from SASOprsk.Risk_l r
left join Sasoprsk.orauser_l u
  on u.user_rk = r.owner_user_rk
left join ( Sasoprsk.ctrl_x_risk_l ctlrsk
  inner join Sasoprsk.Control_inst_l ctrl
   on ctrl.control_inst_rk = ctlrsk.control_inst_rk)
  on ctlrsk.risk_rk = r.risk_rk
left join SASOprsk.orauser_l o
  on r.owner_user_rk=o.user_rk
left join SASOprsk.cust_field_value_l cf1
  on ( r.risk_rk=cf1.business_object_rk and cf1.cust_field_nm = "x_sample_1")
left join SASOprsk.cust_field_value_l cf2
  on ( r.risk_rk=cf2.business_object_rk and cf2.cust_field_nm = "x_sample_2")
left join SASOprsk.cust_field_value_l cf3
  on ( ctrl.control_inst_rk=cf3.business_object_rk and cf3.cust_field_nm = "x_sample_3");
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I write this code by using the minimum data step.&amp;nbsp;I can create a new table for each join and then merge all of them using data step but how can we do the same in data step in a single step. Can we write complex queries in data step like above?&lt;/P&gt;</description>
    <pubDate>Tue, 13 Nov 2018 08:01:49 GMT</pubDate>
    <dc:creator>Azeem112</dc:creator>
    <dc:date>2018-11-13T08:01:49Z</dc:date>
    <item>
      <title>How to write complex queries in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-complex-queries-in-data-step/m-p/512463#M138037</link>
      <description>&lt;P&gt;Dear Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to write some code from proc sql to data step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this is my proc sql code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table Risk_Register as
select distinct risk_id lable 'Risk ID',&lt;BR /&gt; RISK_NM lable = 'Risk Title',&lt;BR /&gt; RISK_DESC lable 'Risk Description',
 ctrl.control_inst_desc lable 'Control Description', &lt;BR /&gt; cf1.value_cd as sample1 label 'sample1&amp;nbsp;',
 cf2.value_cd as sample2&amp;nbsp;label 'sample2',&lt;BR /&gt; cf3.value_cd as sample3&amp;nbsp;label 'sample3',
 u.user_id lable 'User ID',&lt;BR /&gt; u.display_nm lable 'User Name'
from SASOprsk.Risk_l r
left join Sasoprsk.orauser_l u
  on u.user_rk = r.owner_user_rk
left join ( Sasoprsk.ctrl_x_risk_l ctlrsk
  inner join Sasoprsk.Control_inst_l ctrl
   on ctrl.control_inst_rk = ctlrsk.control_inst_rk)
  on ctlrsk.risk_rk = r.risk_rk
left join SASOprsk.orauser_l o
  on r.owner_user_rk=o.user_rk
left join SASOprsk.cust_field_value_l cf1
  on ( r.risk_rk=cf1.business_object_rk and cf1.cust_field_nm = "x_sample_1")
left join SASOprsk.cust_field_value_l cf2
  on ( r.risk_rk=cf2.business_object_rk and cf2.cust_field_nm = "x_sample_2")
left join SASOprsk.cust_field_value_l cf3
  on ( ctrl.control_inst_rk=cf3.business_object_rk and cf3.cust_field_nm = "x_sample_3");
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I write this code by using the minimum data step.&amp;nbsp;I can create a new table for each join and then merge all of them using data step but how can we do the same in data step in a single step. Can we write complex queries in data step like above?&lt;/P&gt;</description>
      <pubDate>Tue, 13 Nov 2018 08:01:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-complex-queries-in-data-step/m-p/512463#M138037</guid>
      <dc:creator>Azeem112</dc:creator>
      <dc:date>2018-11-13T08:01:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to write complex queries in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-complex-queries-in-data-step/m-p/512464#M138038</link>
      <description>&lt;P&gt;No. There's only one by statement allowed in a data step, so you can only combine joins that use the same key variable(s). And you need to prepare your datasets for each merge by doing the necessary sorts.&lt;/P&gt;
&lt;P&gt;It is recommended to do such operations in a sequence of steps. You might even find that a sequence of sort and data steps outperforms one single SQL, sometimes by orders of magnitude.&lt;/P&gt;
&lt;P&gt;On top of that, you have access to the results of intermediate steps, which makes debugging easier.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Nov 2018 08:09:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-complex-queries-in-data-step/m-p/512464#M138038</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-11-13T08:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to write complex queries in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-complex-queries-in-data-step/m-p/512471#M138045</link>
      <description>&lt;P&gt;SQL and SAS are different solutions.&amp;nbsp; SQL is built for relational databases - lots of data stored in many small tables which need merging together.&amp;nbsp; SAS is built for datasets, lots of data in one dataset.&amp;nbsp; So use the appropriate tool for the task.&amp;nbsp; As in this case you have a relational database setup, use SQL to link all those datasets, simplest coding method.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Nov 2018 08:31:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-complex-queries-in-data-step/m-p/512471#M138045</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-11-13T08:31:23Z</dc:date>
    </item>
  </channel>
</rss>

