<?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: code improvement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/code-improvement/m-p/947476#M370910</link>
    <description>&lt;P&gt;It would help if you explained the purpose of the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But let's go through it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why use a LEFT JOIN instead of just use IN operator of SQL with a subquery?&amp;nbsp; The result should be the same unless your intent is to generate multiple copies of the observations from&amp;nbsp;CM_EMAIL_SEND when the same email address appear in multiple observations from the&amp;nbsp;CM_TEST_CUSTOMERS dataset.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But is seems you only want the observations with missing value of email (is that a mistake in your code?).&amp;nbsp; So perhaps what you really meant was NOT in ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table AAA as
select t1.*
  from CM_EMAIL_SEND t1
  where lowcase(t1.email) not in 
   (select lowcase(t2.email) from CM_TEST_CUSTOMERS
    where t2.email is not null)
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Second there is no need to run another query to know how many observations were written to AAA because PROC SQL already wrote that number into the SQLOBS macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let row_count=&amp;amp;sqlobs;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also there is no need to use the %IF code since when AAA has zero observations appending it to&amp;nbsp;&lt;SPAN&gt;CM_EMAIL_CHASOM will not add any observations.&amp;nbsp; If you are worried that it will take too long to recopy all of the existing observation in&amp;nbsp;CM_EMAIL_CHASOM then use some other method to append the records, such as PROC APPEND or a data step with a MODIFY statement.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which implies why not just include the variable&amp;nbsp;&lt;SPAN&gt;SINUN when generating the dataset AAA?&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table AAA as
select t1.*, 'CM' as SINUN
  from CM_EMAIL_SEND t1 ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;And if there is no other need for the AAA dataset then why not just use PROC SQL INSERT statement to add the observations to&amp;nbsp;CM_EMAIL_CHASOM?&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;insert into CM_EMAIL_CHASOM
select t1.*, 'CM' as SINUN
  from CM_EMAIL_SEND t1
  where lowcase(t1.email) not in 
   (select lowcase(t2.email) from CM_TEST_CUSTOMERS where
    t2.email is not null)
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;And finally if the datasets are already sorted by EMAIL (and the values of EMAIL are already converted to lowercase) then forget the SQL and just use data steps.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data aaa;
  merge CM_EMAIL_SEND(in=in1) CM_TEST_CUSTOMERS(in=in2 keep=email);
  by email;
  if in1 and not in2;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 15 Oct 2024 13:32:34 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-10-15T13:32:34Z</dc:date>
    <item>
      <title>code improvement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/code-improvement/m-p/947474#M370909</link>
      <description>Hello, I wrote the following code, I would appreciate it if you could improve it for a shorter and more elegant writing.&lt;BR /&gt;&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;     create table AAA as&lt;BR /&gt;           select t1.*&lt;BR /&gt;                from CM_EMAIL_SEND t1&lt;BR /&gt;                     left join CM_TEST_CUSTOMERS t2&lt;BR /&gt;                     on lowcase(t1.email)=lowcase(t2.email)&lt;BR /&gt;                     where t2.email is null;&lt;BR /&gt;     select count(*) into :row_count from AAA;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;%put the number: &amp;amp;row_count;&lt;BR /&gt;&lt;BR /&gt;%MACRO test;&lt;BR /&gt;     %IF &amp;amp;row_count. gt 0 %THEN&lt;BR /&gt;           %DO;&lt;BR /&gt;                data CM_EMAIL_CHASOM;&lt;BR /&gt;                     set CM_EMAIL_CHASOM AAA(in=new);&lt;BR /&gt;                     if new then&lt;BR /&gt;                           SINUN= 'CM';&lt;BR /&gt;                run;&lt;BR /&gt;&lt;BR /&gt;                proc sql;&lt;BR /&gt;                     delete from CM_EMAIL_SEND&lt;BR /&gt;                     where email in (select email from AAA);&lt;BR /&gt;                quit;&lt;BR /&gt;           %END;&lt;BR /&gt;%MEND test;&lt;BR /&gt;%test;</description>
      <pubDate>Tue, 15 Oct 2024 12:30:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/code-improvement/m-p/947474#M370909</guid>
      <dc:creator>shlomiohana</dc:creator>
      <dc:date>2024-10-15T12:30:03Z</dc:date>
    </item>
    <item>
      <title>Re: code improvement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/code-improvement/m-p/947476#M370910</link>
      <description>&lt;P&gt;It would help if you explained the purpose of the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But let's go through it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why use a LEFT JOIN instead of just use IN operator of SQL with a subquery?&amp;nbsp; The result should be the same unless your intent is to generate multiple copies of the observations from&amp;nbsp;CM_EMAIL_SEND when the same email address appear in multiple observations from the&amp;nbsp;CM_TEST_CUSTOMERS dataset.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But is seems you only want the observations with missing value of email (is that a mistake in your code?).&amp;nbsp; So perhaps what you really meant was NOT in ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table AAA as
select t1.*
  from CM_EMAIL_SEND t1
  where lowcase(t1.email) not in 
   (select lowcase(t2.email) from CM_TEST_CUSTOMERS
    where t2.email is not null)
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Second there is no need to run another query to know how many observations were written to AAA because PROC SQL already wrote that number into the SQLOBS macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let row_count=&amp;amp;sqlobs;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also there is no need to use the %IF code since when AAA has zero observations appending it to&amp;nbsp;&lt;SPAN&gt;CM_EMAIL_CHASOM will not add any observations.&amp;nbsp; If you are worried that it will take too long to recopy all of the existing observation in&amp;nbsp;CM_EMAIL_CHASOM then use some other method to append the records, such as PROC APPEND or a data step with a MODIFY statement.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which implies why not just include the variable&amp;nbsp;&lt;SPAN&gt;SINUN when generating the dataset AAA?&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table AAA as
select t1.*, 'CM' as SINUN
  from CM_EMAIL_SEND t1 ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;And if there is no other need for the AAA dataset then why not just use PROC SQL INSERT statement to add the observations to&amp;nbsp;CM_EMAIL_CHASOM?&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;insert into CM_EMAIL_CHASOM
select t1.*, 'CM' as SINUN
  from CM_EMAIL_SEND t1
  where lowcase(t1.email) not in 
   (select lowcase(t2.email) from CM_TEST_CUSTOMERS where
    t2.email is not null)
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;And finally if the datasets are already sorted by EMAIL (and the values of EMAIL are already converted to lowercase) then forget the SQL and just use data steps.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data aaa;
  merge CM_EMAIL_SEND(in=in1) CM_TEST_CUSTOMERS(in=in2 keep=email);
  by email;
  if in1 and not in2;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2024 13:32:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/code-improvement/m-p/947476#M370910</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-15T13:32:34Z</dc:date>
    </item>
    <item>
      <title>Re: code improvement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/code-improvement/m-p/947477#M370911</link>
      <description>&lt;P&gt;Seriously?&amp;nbsp; You're subsetting on:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where t2.email is null;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But then you want to also check:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; lowcase(t1.email)=lowcase(t2.email&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That's really the same as saying:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where t1.email is null and t2.email is null;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This looks like code that was written to confuse someone rather than a real attempt at writing a program.&amp;nbsp; Can you explain in words what you are trying to make happen?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2024 13:28:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/code-improvement/m-p/947477#M370911</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-10-15T13:28:10Z</dc:date>
    </item>
    <item>
      <title>Re: code improvement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/code-improvement/m-p/947478#M370912</link>
      <description>Hello,&lt;BR /&gt;The goal of the code is to find all the records in the CM_EMAIL_SEND table whose email is not in the CM_TEST_CUSTOMERS table.&lt;BR /&gt; If records are found, they should be added from the CM_EMAIL_SEND table to the CM_EMAIL_CHASOM table, delete the records that were found from the CM_EMAIL_SEND table, SINUN field value should be changed to CM for records passed to the table CM_EMAIL_CHASOM.&lt;BR /&gt;email will be sent saying that records have been transferred between the 2 tables.&lt;BR /&gt;Thanks.</description>
      <pubDate>Tue, 15 Oct 2024 13:38:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/code-improvement/m-p/947478#M370912</guid>
      <dc:creator>shlomiohana</dc:creator>
      <dc:date>2024-10-15T13:38:48Z</dc:date>
    </item>
  </channel>
</rss>

