<?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 sas update table if then procedure in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/399651#M66657</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have initial code which is in a macro function where vairable 'Records' is defined before.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%if &amp;amp;Records &amp;gt; 5 %then %do;&lt;BR /&gt;data Work.Table_1(replace=yes);&lt;BR /&gt;set Work.Table_2;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table_1 already exists. And if 'Records &amp;gt; 5' then replace it with Table_2; and Table_1 remains if 'Records &amp;lt;= 5'.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My question is: I am not using macro function, but general procedure (data step). How could I write the code?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 28 Sep 2017 21:38:51 GMT</pubDate>
    <dc:creator>Crubal</dc:creator>
    <dc:date>2017-09-28T21:38:51Z</dc:date>
    <item>
      <title>sas update table if then procedure</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/399651#M66657</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have initial code which is in a macro function where vairable 'Records' is defined before.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%if &amp;amp;Records &amp;gt; 5 %then %do;&lt;BR /&gt;data Work.Table_1(replace=yes);&lt;BR /&gt;set Work.Table_2;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table_1 already exists. And if 'Records &amp;gt; 5' then replace it with Table_2; and Table_1 remains if 'Records &amp;lt;= 5'.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My question is: I am not using macro function, but general procedure (data step). How could I write the code?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2017 21:38:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/399651#M66657</guid>
      <dc:creator>Crubal</dc:creator>
      <dc:date>2017-09-28T21:38:51Z</dc:date>
    </item>
    <item>
      <title>Re: sas update table if then procedure</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/399653#M66658</link>
      <description>&lt;P&gt;First the macro %IF can only be utilized inside a defined macro, which you have not shown.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What are you trying to do?&amp;nbsp; Are you creating table2 only when some variable has at least one obs in table1 with a value &amp;gt; 5?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2017 21:53:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/399653#M66658</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-09-28T21:53:37Z</dc:date>
    </item>
    <item>
      <title>Re: sas update table if then procedure</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/399675#M66659</link>
      <description>&lt;P&gt;Try this...&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ReplaceWhenTrue;
%if %eval(&amp;amp;Records&amp;gt;5) %then %do;
  data table1;
     set table2;
  run;
%end;
%mend;

%ReplaceWhenTrue;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Sep 2017 01:41:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/399675#M66659</guid>
      <dc:creator>ShiroAmada</dc:creator>
      <dc:date>2017-09-29T01:41:22Z</dc:date>
    </item>
    <item>
      <title>Re: sas update table if then procedure</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/399781#M66665</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/160362"&gt;@ShiroAmada&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works. Can I write the code which is the same as you did without using a macro function?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Sep 2017 13:24:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/399781#M66665</guid>
      <dc:creator>Crubal</dc:creator>
      <dc:date>2017-09-29T13:24:57Z</dc:date>
    </item>
    <item>
      <title>Re: sas update table if then procedure</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/399783#M66666</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is within a macro function that I omitted the part.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro step_1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%if &amp;amp;Records &amp;gt; 5 %then %do;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;data Work.Table_1(replace=yes);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;set Work.Table_2;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%step_1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to replace Table_1 if 'Recodrs&amp;gt;5' in Table_2, where 'Records' is a variable defiend before; and not replace Table_1 if this condition not satisfied.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Sep 2017 13:37:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/399783#M66666</guid>
      <dc:creator>Crubal</dc:creator>
      <dc:date>2017-09-29T13:37:41Z</dc:date>
    </item>
    <item>
      <title>Re: sas update table if then procedure</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/400189#M66697</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/50019"&gt;@Crubal&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/160362"&gt;@ShiroAmada&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works. Can I write the code which is the same as you did without using a macro function?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;During compilation each set statement fills the pdv with the variables of the datasets listed in the statement. I don't know any way to circumvent this. If both datasets have the same structure you can use&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;   
   if _nobs_ &amp;gt; 5 then 
      set table2 nobs=_nobs_;
   else 
      set table1;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This step always rewrites table1 ... not the best idea.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2017 06:13:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/400189#M66697</guid>
      <dc:creator>error_prone</dc:creator>
      <dc:date>2017-10-02T06:13:56Z</dc:date>
    </item>
    <item>
      <title>Re: sas update table if then procedure</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/400583#M66724</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/43025"&gt;@error_prone&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks! I like the idea. As you mentioned they have the same data structure.&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I changed the code a little bit,&amp;nbsp;because 'table1' may not exist for the first time, but it will exist after running this part:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;   
   if _nobs_ &amp;gt; 5 then 
      set table2 nobs=_nobs_;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this way, 'table1' does not exist initially, so it is same as 'table2'. And after updating, 'table1' will become 'table2' if _nobs_ &amp;gt; 5.&lt;/P&gt;&lt;P&gt;Is that right?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Oct 2017 13:17:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/400583#M66724</guid>
      <dc:creator>Crubal</dc:creator>
      <dc:date>2017-10-03T13:17:43Z</dc:date>
    </item>
    <item>
      <title>Re: sas update table if then procedure</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/400592#M66726</link>
      <description>&lt;P&gt;A simple way to conditionally generate code when not using a macro is to use CALL EXECUTE.&amp;nbsp; So assuming that you still have the macro variable RECORDS defined as before then you could use this data step to conditionally generate the data step that replaces TABLE_1.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  if  &amp;amp;Records &amp;gt; 5 then do;
    call execute(
'data Work.Table_1(replace=yes);
 set Work.Table_2;
 run;
'
    );
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Oct 2017 13:57:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/400592#M66726</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-10-03T13:57:58Z</dc:date>
    </item>
    <item>
      <title>Re: sas update table if then procedure</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/400733#M66730</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/50019"&gt;@Crubal&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this way, 'table1' does not exist initially, so it is same as 'table2'. And after updating, 'table1' will become 'table2' if _nobs_ &amp;gt; 5.&lt;/P&gt;&lt;P&gt;Is that right?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You could just test your code and see what's happening &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I would not recommend removing the else-statement.&amp;nbsp;&lt;/P&gt;&lt;P&gt;In a production process, i would use either use the solution posted by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;or keep the macro.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Oct 2017 18:58:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/400733#M66730</guid>
      <dc:creator>error_prone</dc:creator>
      <dc:date>2017-10-03T18:58:41Z</dc:date>
    </item>
    <item>
      <title>Re: sas update table if then procedure</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/400972#M66740</link>
      <description>&lt;P&gt;Got it, thanks!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2017 13:42:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/sas-update-table-if-then-procedure/m-p/400972#M66740</guid>
      <dc:creator>Crubal</dc:creator>
      <dc:date>2017-10-04T13:42:10Z</dc:date>
    </item>
  </channel>
</rss>

