<?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 improve my code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-code/m-p/948270#M371059</link>
    <description>&lt;P&gt;Didn't you already ask this question??&lt;/P&gt;
&lt;P&gt;First let's make some example datasets so we have something to program against.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cm_email_send ;
  input email :$40. var1 ;
cards;
a@b.com 1
b@c.com 2
c@d.com 3
;

data cm_test_customers;
  input email :$40. ;
cards;
a@b.com
d@e.com
;

data cm_email_chasom;
  if 0 then set cm_email_send (obs=0);
  length SINUN $2 ;
  input email var1 sinun;
cards;
x@y.com 4 xx
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So first thing is that PROC SQL will count for you, so no need to count twice.&lt;/P&gt;
&lt;P&gt;And really from what you describe there is no need to make a temporary copy of the data to be moved.&amp;nbsp; Just insert it directly into the target dataset.&amp;nbsp; Also there is no need to define a macro run simple %IF/%THEN/%END blocks of code.&amp;nbsp; You can do that now in open SAS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
%let nobs=0;
  insert into cm_email_chasom
  select a.*,'CM' as sinun 
   from cm_email_send a
   where lowcase(a.email) not in (select lowcase(b.email) from cm_test_customers b)
  ;
%let nobs=&amp;amp;sqlobs;
%if &amp;amp;nobs %then %do;
  delete from cm_email_send a
   where lowcase(a.email) not in (select lowcase(b.email) from cm_test_customers b)
  ;
%end;
quit;

%if &amp;amp;nobs %then %do;
  %put Send email saying that &amp;amp;nobs records were transferred. ;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So let's see how it worked.&lt;/P&gt;
&lt;PRE&gt;270  %if &amp;amp;nobs %then %do;
271    %put Send email saying that &amp;amp;nobs records were transferred. ;
Send email saying that 2 records were transferred.
272  %end;
&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1729392152757.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101584iE7B064A1C44654A2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1729392152757.png" alt="Tom_0-1729392152757.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_1-1729392172949.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101585i46F37F102A595123/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_1-1729392172949.png" alt="Tom_1-1729392172949.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 20 Oct 2024 02:43:39 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-10-20T02:43:39Z</dc:date>
    <item>
      <title>How to improve my code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-code/m-p/948255#M371051</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hello, &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I wrote the following code, I would appreciate it if you could improve it for a shorter and more elegant writing.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The code's goal is to find all the records in the CM_EMAIL_SEND table whose emails are 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;an email will be sent saying that records have been transferred between the 2 tables.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
create table AAA as
select t1.*
from CM_EMAIL_SEND t1
left join CM_TEST_CUSTOMERS t2
on lowcase(t1.email)=lowcase(t2.email)
where t2.email is null;
select count(*) into :row_count from AAA;
quit;

%put the number: &amp;amp;row_count;

%MACRO test;
%IF &amp;amp;row_count. gt 0 %THEN
%DO;
data CM_EMAIL_CHASOM;
set CM_EMAIL_CHASOM AAA(in=new);
if new then
SINUN= 'CM';
run;

proc sql;
delete from CM_EMAIL_SEND
where email in (select email from AAA);
quit;
%END;
%MEND test;
%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Oct 2024 18:56:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-code/m-p/948255#M371051</guid>
      <dc:creator>shlomiohana</dc:creator>
      <dc:date>2024-10-19T18:56:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve my code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-code/m-p/948259#M371055</link>
      <description>&lt;P&gt;I don't see a lot that needs to be improved here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can get away with not creating a macro in this case, the %IF %THEN %DO and %END is allowed in open code in many situations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only other thing that jumps out at me (which is really very trivial) is here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;where the semi-colon is unnecessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In terms of style, you want to indent the code within PROCs and within data steps properly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Oct 2024 20:05:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-code/m-p/948259#M371055</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-10-19T20:05:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve my code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-code/m-p/948264#M371058</link>
      <description>&lt;P&gt;Assuming I understodd what you mean.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table CM_EMAIL_CHASOM_2 as
select * from CM_EMAIL_CHASOM
outer union corr
select *,'CM' as SINUN from CM_EMAIL_SEND 
 where lowcase(email) not in (select lowcase(email) from CM_TEST_CUSTOMERS);

delete from CM_EMAIL_SEND
where lowcase(email) in (
 select lowcase(email) from CM_EMAIL_SEND
  where lowcase(email) not in (select lowcase(email) from CM_TEST_CUSTOMERS)
);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 20 Oct 2024 01:53:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-code/m-p/948264#M371058</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-10-20T01:53:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to improve my code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-code/m-p/948270#M371059</link>
      <description>&lt;P&gt;Didn't you already ask this question??&lt;/P&gt;
&lt;P&gt;First let's make some example datasets so we have something to program against.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cm_email_send ;
  input email :$40. var1 ;
cards;
a@b.com 1
b@c.com 2
c@d.com 3
;

data cm_test_customers;
  input email :$40. ;
cards;
a@b.com
d@e.com
;

data cm_email_chasom;
  if 0 then set cm_email_send (obs=0);
  length SINUN $2 ;
  input email var1 sinun;
cards;
x@y.com 4 xx
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So first thing is that PROC SQL will count for you, so no need to count twice.&lt;/P&gt;
&lt;P&gt;And really from what you describe there is no need to make a temporary copy of the data to be moved.&amp;nbsp; Just insert it directly into the target dataset.&amp;nbsp; Also there is no need to define a macro run simple %IF/%THEN/%END blocks of code.&amp;nbsp; You can do that now in open SAS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
%let nobs=0;
  insert into cm_email_chasom
  select a.*,'CM' as sinun 
   from cm_email_send a
   where lowcase(a.email) not in (select lowcase(b.email) from cm_test_customers b)
  ;
%let nobs=&amp;amp;sqlobs;
%if &amp;amp;nobs %then %do;
  delete from cm_email_send a
   where lowcase(a.email) not in (select lowcase(b.email) from cm_test_customers b)
  ;
%end;
quit;

%if &amp;amp;nobs %then %do;
  %put Send email saying that &amp;amp;nobs records were transferred. ;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So let's see how it worked.&lt;/P&gt;
&lt;PRE&gt;270  %if &amp;amp;nobs %then %do;
271    %put Send email saying that &amp;amp;nobs records were transferred. ;
Send email saying that 2 records were transferred.
272  %end;
&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1729392152757.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101584iE7B064A1C44654A2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1729392152757.png" alt="Tom_0-1729392152757.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_1-1729392172949.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101585i46F37F102A595123/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_1-1729392172949.png" alt="Tom_1-1729392172949.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Oct 2024 02:43:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-improve-my-code/m-p/948270#M371059</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-20T02:43:39Z</dc:date>
    </item>
  </channel>
</rss>

