<?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 How do I use a macro withing SQL code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-macro-withing-SQL-code/m-p/630619#M186699</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to macro-ise the following code, I have a lot of bank name cleansing to do beyond these 2 names&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table Bank_cleansed as select *,&lt;BR /&gt;(case when (address_name contains "Bank of FR")&lt;BR /&gt;then "Bank of France"&lt;BR /&gt;when (address_name contains "Bank of DM")&lt;BR /&gt;then "Bank of Germany"&lt;BR /&gt;else address_name&lt;BR /&gt;end) as new_address_name&lt;BR /&gt;from Bankfile;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not quite there with the macro:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro addname(string,fullname)&lt;BR /&gt;proc sql;&lt;BR /&gt;create table Bank_cleansed&lt;BR /&gt;as select *,&lt;BR /&gt;(case when (address_name contains &amp;amp;string.)&lt;BR /&gt;then &amp;amp;fullname.&lt;BR /&gt;else address_name&lt;BR /&gt;end)&lt;BR /&gt;as new_address_name&lt;BR /&gt;from Bankfile;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;BR /&gt;%addname("Bank of FR","Bank of France");&lt;BR /&gt;%addname("Bank of DM","Bank of Germany");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm thinking the code needs to be somehow embedded in the macro? Any help would be great.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks! Liz&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 09 Mar 2020 13:01:53 GMT</pubDate>
    <dc:creator>Liz_P</dc:creator>
    <dc:date>2020-03-09T13:01:53Z</dc:date>
    <item>
      <title>How do I use a macro withing SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-macro-withing-SQL-code/m-p/630619#M186699</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to macro-ise the following code, I have a lot of bank name cleansing to do beyond these 2 names&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table Bank_cleansed as select *,&lt;BR /&gt;(case when (address_name contains "Bank of FR")&lt;BR /&gt;then "Bank of France"&lt;BR /&gt;when (address_name contains "Bank of DM")&lt;BR /&gt;then "Bank of Germany"&lt;BR /&gt;else address_name&lt;BR /&gt;end) as new_address_name&lt;BR /&gt;from Bankfile;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not quite there with the macro:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro addname(string,fullname)&lt;BR /&gt;proc sql;&lt;BR /&gt;create table Bank_cleansed&lt;BR /&gt;as select *,&lt;BR /&gt;(case when (address_name contains &amp;amp;string.)&lt;BR /&gt;then &amp;amp;fullname.&lt;BR /&gt;else address_name&lt;BR /&gt;end)&lt;BR /&gt;as new_address_name&lt;BR /&gt;from Bankfile;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;BR /&gt;%addname("Bank of FR","Bank of France");&lt;BR /&gt;%addname("Bank of DM","Bank of Germany");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm thinking the code needs to be somehow embedded in the macro? Any help would be great.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks! Liz&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Mar 2020 13:01:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-macro-withing-SQL-code/m-p/630619#M186699</guid>
      <dc:creator>Liz_P</dc:creator>
      <dc:date>2020-03-09T13:01:53Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a macro withing SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-macro-withing-SQL-code/m-p/630622#M186702</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/315772"&gt;@Liz_P&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not quite there with the macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What is the problem you are having?&lt;/P&gt;</description>
      <pubDate>Mon, 09 Mar 2020 13:09:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-macro-withing-SQL-code/m-p/630622#M186702</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-03-09T13:09:32Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a macro withing SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-macro-withing-SQL-code/m-p/630623#M186703</link>
      <description>&lt;P&gt;Your macro looks fine, but your logic is flawed.&amp;nbsp; Each time you call it you are reading the same input and writing to the same output.&amp;nbsp; So the second run overwrite the results of the first run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you feel the need for making a macro?&amp;nbsp; Why not just write the code instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table Bank_cleansed as 
select *
     , case when (address_name contains "Bank of FR") then "Bank of France"
            when (address_name contains "Bank of DM") then "Bank of Germany"
            else address_name
       end as new_address_name
from Bankfile
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What is the source of the rename lists?&amp;nbsp; Do you have it in a dataset somewhere?&amp;nbsp; So if you had a dataset named FIXED_ADDRESS with variables ADDRESS_NAME and FIXED_NAME then your code might look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table Bank_cleansed as 
select a.*
     , coalesce(b.fixed_name,a.address_name) as new_address_name
from Bankfile
left join Fixed_address b
on a.address_name contains b.address_name
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Mar 2020 13:27:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-macro-withing-SQL-code/m-p/630623#M186703</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-03-09T13:27:30Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a macro withing SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-macro-withing-SQL-code/m-p/630626#M186705</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By calling a macro for each renaming, you will have to reprocess the entire dataset each time.&lt;/P&gt;
&lt;P&gt;Here is a solution using a data step rather than proc sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data clean_names;
    infile cards dlm=',' dsd;
	length name clean_name $50;
    input name clean_name;
    cards;
Bank of FR, Bank of France
Bank of DM, Bank of Germany
;
run;

data have;
    input address_name $50.;
    cards;
foo Bank of FR bar
baz Bank of DM qux
;
run;

proc sql noprint;
    SELECT nobs
    INTO :nbanks
    FROM dictionary.tables
    WHERE LIBNAME="WORK" AND MEMNAME="HAVE";
quit;

data want;
    array names(&amp;amp;nbanks.) $50. _TEMPORARY_;
    array clean(&amp;amp;nbanks.) $50. _TEMPORARY_;

    if _N_=1 then do i=1 by 1 until(endcn);
        set clean_names end=endcn;
        names(i)=name;
        clean(i)=clean_name;
    end;

    set have;

    new_address_name=address_name;

    do i=1 to dim(names);
        if index(address_name, strip(names(i))) then do;
            new_address_name=clean(i);
            leave;
        end;
    end;

    keep address_name new_address_name;
run;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Mar 2020 13:33:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-macro-withing-SQL-code/m-p/630626#M186705</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-03-09T13:33:29Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a macro withing SQL code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-macro-withing-SQL-code/m-p/630647#M186712</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The reason I didn't want to put so much code as &amp;gt;100 bank names to clean. Storing as a dataset and merging in works a treat. Thankyou &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Mar 2020 14:33:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-macro-withing-SQL-code/m-p/630647#M186712</guid>
      <dc:creator>Liz_P</dc:creator>
      <dc:date>2020-03-09T14:33:38Z</dc:date>
    </item>
  </channel>
</rss>

