<?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 Convert SQL query to Macro Proc SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Convert-SQL-query-to-Macro-Proc-SQL/m-p/517975#M140129</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I newly started to work in Proc SQL,&lt;/P&gt;&lt;P&gt;Even though I successfully done with the below code, which have a simple condition in SET statement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro myUpd2(tbn, vart, varf, from);

proc sql;
update &amp;amp;tbn. as b
set &amp;amp;vart. = (select &amp;amp;varf. from &amp;amp;from. where 
b.Mno = Patient_id and record_date = (select max(record_date) from &amp;amp;from. where b.Mno = Patient_ID)
)
where &amp;amp;vart. is null;
quit;
%mend myUpd2;

%myUpd2(Proj.flsha, aad, diab_ageonset, Mvdsc.medical1);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I failed on the SET statement that given below to write in ProcSQL.&lt;/P&gt;&lt;P&gt;I need some guidance to solve the given code which using UNION statements.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;update Tab1
set fvfbs = b.Test_Result_Value
FROM Tab1 a JOIN (select * from test_results union select * from test_results_archive) b 
ON a.mno = b.patient_id and fvfbs is null and
b.record_date = (select max(record_date) from (select * from test_results where param_id = 'fbs' union select * from test_results_archive where param_id = 'fbs') cc where cc.patient_id = a.mno) and
param_id = 'fbs' &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;Thanks in advance!&lt;/P&gt;</description>
    <pubDate>Tue, 04 Dec 2018 07:00:37 GMT</pubDate>
    <dc:creator>Sathish_jammy</dc:creator>
    <dc:date>2018-12-04T07:00:37Z</dc:date>
    <item>
      <title>Convert SQL query to Macro Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-SQL-query-to-Macro-Proc-SQL/m-p/517975#M140129</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I newly started to work in Proc SQL,&lt;/P&gt;&lt;P&gt;Even though I successfully done with the below code, which have a simple condition in SET statement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro myUpd2(tbn, vart, varf, from);

proc sql;
update &amp;amp;tbn. as b
set &amp;amp;vart. = (select &amp;amp;varf. from &amp;amp;from. where 
b.Mno = Patient_id and record_date = (select max(record_date) from &amp;amp;from. where b.Mno = Patient_ID)
)
where &amp;amp;vart. is null;
quit;
%mend myUpd2;

%myUpd2(Proj.flsha, aad, diab_ageonset, Mvdsc.medical1);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I failed on the SET statement that given below to write in ProcSQL.&lt;/P&gt;&lt;P&gt;I need some guidance to solve the given code which using UNION statements.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;update Tab1
set fvfbs = b.Test_Result_Value
FROM Tab1 a JOIN (select * from test_results union select * from test_results_archive) b 
ON a.mno = b.patient_id and fvfbs is null and
b.record_date = (select max(record_date) from (select * from test_results where param_id = 'fbs' union select * from test_results_archive where param_id = 'fbs') cc where cc.patient_id = a.mno) and
param_id = 'fbs' &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Tue, 04 Dec 2018 07:00:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-SQL-query-to-Macro-Proc-SQL/m-p/517975#M140129</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2018-12-04T07:00:37Z</dc:date>
    </item>
    <item>
      <title>Re: Convert SQL query to Macro Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-SQL-query-to-Macro-Proc-SQL/m-p/517979#M140130</link>
      <description>&lt;P&gt;PROC SQL updates require a separate sub-query for each SET column as demonstrated in this post:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Faster-way-to-write-a-SQL-Update-Query/td-p/166222" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Faster-way-to-write-a-SQL-Update-Query/td-p/166222&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's why many SAS programmers prefer other techniques like MODIFY.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 07:30:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-SQL-query-to-Macro-Proc-SQL/m-p/517979#M140130</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2018-12-03T07:30:01Z</dc:date>
    </item>
    <item>
      <title>Re: Convert SQL query to Macro Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-SQL-query-to-Macro-Proc-SQL/m-p/517987#M140134</link>
      <description>&lt;P&gt;"&lt;SPAN&gt;I newly started to work in Proc SQL, I'm good in SQL codes but not in PROC SQL.&lt;/SPAN&gt;" - statement does not make sense.&amp;nbsp; Proc SQL implements ANSI SQL which is the basis for most SQL compilers out there, which if you know one you know the rest.&amp;nbsp; Therefore you either know it or you don't.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second off, don't jump into writing macro code.&amp;nbsp; Write base SAS first, then if there is benefits to it, convert it into a macro.&amp;nbsp; There are benefits to using Macro, however in most cases I see its used in trying to replace Base SAS which just results in messy unmaintable code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Third. Learn SAS, it is the language you are using here.&amp;nbsp; SQL can have a use in SAS, but learning the actual language will help you write simpler, easier to maintain, and more efficient code.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 08:44:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-SQL-query-to-Macro-Proc-SQL/m-p/517987#M140134</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-12-03T08:44:38Z</dc:date>
    </item>
  </channel>
</rss>

