<?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: How to implement a macro in update query of Proc SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-in-update-query-of-Proc-SQL/m-p/515496#M139090</link>
    <description>&lt;P&gt;The steps of macro development:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;get working SAS code&lt;BR /&gt;-&amp;gt; you have done that already&lt;/LI&gt;
&lt;LI&gt;identify the parts that need to be made dynamic&lt;BR /&gt;-&amp;gt; compare your different steps; in your case, it will be dataset and variable names&lt;/LI&gt;
&lt;LI&gt;replace those with macro variables, set the macro variables with %let, and test the code again&lt;/LI&gt;
&lt;LI&gt;wrap the code into a macro definition, insert the macro variables as parameters, and remove the %let's&lt;/LI&gt;
&lt;LI&gt;test the macro with the values set as parameters; use options mlogic mprint symbolgen;&lt;/LI&gt;
&lt;/OL&gt;</description>
    <pubDate>Fri, 23 Nov 2018 07:10:25 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-11-23T07:10:25Z</dc:date>
    <item>
      <title>How to implement a macro in update query of Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-in-update-query-of-Proc-SQL/m-p/515489#M139085</link>
      <description>&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Helvetica',sans-serif; color: #333333;"&gt;Hi Community,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Helvetica',sans-serif; color: #333333;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Helvetica',sans-serif; color: #333333;"&gt;Here I have an update query which I used often in same method on different tasks.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Helvetica',sans-serif; color: #333333;"&gt;I need a macro to utilize the update query in further days.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tab1; 
input ID height waist; 
cards; 
1 . .
2 . . 
3 . . 
; 
run; 

data tab2; 
input p_id record_date :yymmdd10. Height; 
format record_date yymmddd10.; cards; 
1 2018-10-18 10 
1 2018-10-20 12 
2 2018-10-18 12 
2 2018-10-20 13 
3 2017-09-11 15 
; 
run;

data tab3;
input p_id record_date :yymmdd10. Waist; 
format record_date yymmddd10.; 
cards; 
1 2017-10-18 50 
1 2016-10-20 45 
2 2018-10-18 30 
2 2018-10-20 28 
3 2017-09-11 60 
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In the Tab1 I need to update the Min date value of Height,Waist from Tab2 and Tab3.&lt;/P&gt;&lt;P&gt;The given update query will resolve the task. I rerun it on daily basis for different table IDs'.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
update Tab1 a
set height = (
  select b.Height
  from tab2 b
  where a.ID = b.p_ID and b.record_date = (select min(record_date) from Tab2 c where a.ID = c.p_ID)
)
where height is null;
quit;

Proc sql;
update Tab1 a
set waist = (
  select b.Waist
  from tab3 b
  where a.ID = b.p_ID and b.record_date = (select min(record_date) from Tab3 c where a.ID = c.p_ID)
)
where waist is null;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I don't know how to implement a macro in the update code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please suggest me a macro code for the update query.&lt;/P&gt;&lt;P&gt;Thanks in Advance!&lt;/P&gt;</description>
      <pubDate>Fri, 23 Nov 2018 05:39:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-in-update-query-of-Proc-SQL/m-p/515489#M139085</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2018-11-23T05:39:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement a macro in update query of Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-in-update-query-of-Proc-SQL/m-p/515493#M139089</link>
      <description>&lt;P&gt;Should be something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro myUpdate(dsn, var, from);
proc sql;
update &amp;amp;dsn. as a
set &amp;amp;var. = (
  select &amp;amp;var.
  from &amp;amp;from.
  where a.ID = p_ID and record_date = (select min(record_date) from &amp;amp;from. where a.ID = p_ID)
)
where &amp;amp;var. is null;
quit;
%mend myUpdate;

%myUpdate(Tab1, Height, Tab2);
%myUpdate(Tab1, Waist, Tab3);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(untested)&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Nov 2018 06:31:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-in-update-query-of-Proc-SQL/m-p/515493#M139089</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-11-23T06:31:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement a macro in update query of Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-in-update-query-of-Proc-SQL/m-p/515496#M139090</link>
      <description>&lt;P&gt;The steps of macro development:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;get working SAS code&lt;BR /&gt;-&amp;gt; you have done that already&lt;/LI&gt;
&lt;LI&gt;identify the parts that need to be made dynamic&lt;BR /&gt;-&amp;gt; compare your different steps; in your case, it will be dataset and variable names&lt;/LI&gt;
&lt;LI&gt;replace those with macro variables, set the macro variables with %let, and test the code again&lt;/LI&gt;
&lt;LI&gt;wrap the code into a macro definition, insert the macro variables as parameters, and remove the %let's&lt;/LI&gt;
&lt;LI&gt;test the macro with the values set as parameters; use options mlogic mprint symbolgen;&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Fri, 23 Nov 2018 07:10:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-in-update-query-of-Proc-SQL/m-p/515496#M139090</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-11-23T07:10:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement a macro in update query of Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-in-update-query-of-Proc-SQL/m-p/517367#M139853</link>
      <description>&lt;P&gt;You are Awesome Buddy!&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;What you did for me was kind of a big deal. Thank you so much &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Nov 2018 06:08:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-in-update-query-of-Proc-SQL/m-p/517367#M139853</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2018-11-30T06:08:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement a macro in update query of Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-in-update-query-of-Proc-SQL/m-p/517980#M140131</link>
      <description>&lt;P&gt;Dear&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for taking your valuable time to throughly review my post.&lt;/P&gt;&lt;P&gt;The code you suggested works well. I have a little complication in the code when it comes to different condition.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I failed on the SET statement that given below. While I try, Its really quiet different to write a code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;UPDATE Tab1
SET fvNPDR_lt = Is_RD_NPDR_Left,
fvNPDR_rt = Is_RD_NPDR_Right,
fvPDR_Lt = Is_RD_PDR_Left,
fvPDR_rt = Is_RD_PDR_Right
FROM Tab1 a join eye_diagnosis b
on a.mno = b.patient_id and fvNPDR_lt is null and fvNPDR_rt is null and fvPDR_Lt is null and fvPDR_rt is null and  
b.Advice_Date = (select max(Advice_Date) from eye_diagnosis cc where cc.patient_id = a.mno)


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;I need some guidance to solve the given code which using UNION, NULL statements by utilizing MACRO.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 08:01:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-in-update-query-of-Proc-SQL/m-p/517980#M140131</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2018-12-03T08:01:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement a macro in update query of Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-in-update-query-of-Proc-SQL/m-p/518202#M140216</link>
      <description>&lt;P&gt;You cannot update the tables involved in a join with SAS/SQL. To update the records where all 4 fv* variables are missing with the values from table eye_diagnosis corresponding to the latest advice_date, you would need something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;UPDATE Tab1 as a
SET 
    fvNPDR_lt = (select Is_RD_NPDR_Left from eye_diagnosis where patient_id=a.mno having advice_date=max(advice_date)),
    fvNPDR_rt = (select Is_RD_NPDR_Right from eye_diagnosis where patient_id=a.mno having advice_date=max(advice_date)),
    fvPDR_Lt = (select Is_RD_PDR_Left from eye_diagnosis where patient_id=a.mno having advice_date=max(advice_date)),
    fvPDR_rt = (select Is_RD_PDR_Right from eye_diagnosis where patient_id=a.mno having advice_date=max(advice_date))
where fvNPDR_lt is null and fvNPDR_rt is null and fvPDR_Lt is null and fvPDR_rt is null;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Dec 2018 19:23:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-in-update-query-of-Proc-SQL/m-p/518202#M140216</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-12-03T19:23:07Z</dc:date>
    </item>
  </channel>
</rss>

