<?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: Replacing data from a different table in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Replacing-data-from-a-different-table/m-p/676581#M23722</link>
    <description>&lt;P&gt;Wow! excellent!&lt;/P&gt;
&lt;P&gt;Is there any way to use do loop for the following. Because some of the city names have about 50 incorrect names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;left join have2 b
        on (a.city_name = b.incorrect_name1
        or a.city_name = b.incorrect_name2
        or a.city_name = b.incorrect_name3
        or a.city_name = b.incorrect_name4
        or a.city_name = b.incorrect_name5)&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 13 Aug 2020 19:07:52 GMT</pubDate>
    <dc:creator>Barkat</dc:creator>
    <dc:date>2020-08-13T19:07:52Z</dc:date>
    <item>
      <title>Replacing data from a different table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replacing-data-from-a-different-table/m-p/676547#M23708</link>
      <description>&lt;P&gt;I have two tables as below.&lt;/P&gt;
&lt;P&gt;have1 has three columns. name, date and city_name&lt;/P&gt;
&lt;P&gt;have2 has six columns. First col has correct city names. Later columns have listed incorrect city names.&lt;/P&gt;
&lt;P&gt;How can I replace misspelled city_name in have1 from correct name in have2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: somehow have2 table is not creating properly&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have1;&lt;BR /&gt;infile datalines dsd dlm=',' ;&lt;BR /&gt;input name :$6. id :$3. date :MMDDYY10. city_name :$12. ;&lt;BR /&gt;datalines;&lt;BR /&gt;Max,010,01/10/2017,Almeda&lt;BR /&gt;Robin,023,.,Sacramanto&lt;BR /&gt;Sandra,125,08/10/2013,LA&lt;BR /&gt;Bob,200,01/12/2016,Woodland&lt;BR /&gt;John,202,05/30/2014,L.A.&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data have2;&lt;BR /&gt;infile datalines dsd dlm=',' ;&lt;BR /&gt;input correct_name :$15. correct_name :$15. incorrect_name1 :$15. incorrect_name2 :$15. incorrect_name3 :$15. incorrect_name4 :$15. incorrect_name5 :$15. ;&lt;BR /&gt;datalines;&lt;BR /&gt;Sacramento,Sacraminto,Sacrament,Sacramintu,Scraminto,Sacramanto&lt;BR /&gt;Los Angeles,Loss Angeles,LA,LAngeles,L.A.,-&lt;BR /&gt;Woodland,Woodland,Woodland,-,-,-&lt;BR /&gt;Richmond,Rchmond,Richmand,Richmon,-,-&lt;BR /&gt;Alameda,Almeda,-,-,-,-&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Aug 2020 16:52:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replacing-data-from-a-different-table/m-p/676547#M23708</guid>
      <dc:creator>Barkat</dc:creator>
      <dc:date>2020-08-13T16:52:09Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing data from a different table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replacing-data-from-a-different-table/m-p/676569#M23716</link>
      <description>&lt;P&gt;This should give what you're looking for:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
    infile datalines dsd dlm=',' ;
    input name :$6. id :$3. date :MMDDYY10. city_name :$12. ;
    datalines;
    Max,010,01/10/2017,Almeda
    Robin,023,.,Sacramanto
    Sandra,125,08/10/2013,LA
    Bob,200,01/12/2016,Woodland
    John,202,05/30/2014,L.A.
    ;
run;


data have2;
    infile datalines dsd dlm=',' ;
    input correct_name :$15. incorrect_name1 :$15. incorrect_name2 :$15. incorrect_name3 :$15. incorrect_name4 :$15. incorrect_name5 :$15. ;
    datalines;
    Sacramento, Sacraminto, Sacrament, Sacramintu, Scraminto, Sacramanto
    Los Angeles,Loss Angeles,LA,LAngeles,L.A.,-
    Woodland,Woodland,Woodland,-,-,-
    Richmond,Rchmond,Richmand,Richmon,-,-
    Alameda,Almeda,-,-,-,-
    ;
run;

proc sql;
    create table want as
    select a.name
          ,a.id
          ,a.date format MMDDYY10.
          ,case 
               when b.correct_name is null
                   then a.city_name
               else b.correct_name
           end as city_name
    from have1 a
    left join have2 b
        on (a.city_name = b.incorrect_name1
        or a.city_name = b.incorrect_name2
        or a.city_name = b.incorrect_name3
        or a.city_name = b.incorrect_name4
        or a.city_name = b.incorrect_name5)
    ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Aug 2020 18:07:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replacing-data-from-a-different-table/m-p/676569#M23716</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2020-08-13T18:07:29Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing data from a different table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replacing-data-from-a-different-table/m-p/676581#M23722</link>
      <description>&lt;P&gt;Wow! excellent!&lt;/P&gt;
&lt;P&gt;Is there any way to use do loop for the following. Because some of the city names have about 50 incorrect names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;left join have2 b
        on (a.city_name = b.incorrect_name1
        or a.city_name = b.incorrect_name2
        or a.city_name = b.incorrect_name3
        or a.city_name = b.incorrect_name4
        or a.city_name = b.incorrect_name5)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Aug 2020 19:07:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replacing-data-from-a-different-table/m-p/676581#M23722</guid>
      <dc:creator>Barkat</dc:creator>
      <dc:date>2020-08-13T19:07:52Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing data from a different table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replacing-data-from-a-different-table/m-p/676589#M23723</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/335985"&gt;@Barkat&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Wow! excellent!&lt;/P&gt;
&lt;P&gt;Is there any way to use do loop for the following. Because some of the city names have about 50 incorrect names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;left join have2 b
        on (a.city_name = b.incorrect_name1
        or a.city_name = b.incorrect_name2
        or a.city_name = b.incorrect_name3
        or a.city_name = b.incorrect_name4
        or a.city_name = b.incorrect_name5)&lt;/LI-CODE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;First think would be to create a data set that has one record per correct_name and incorrect_name.&lt;/P&gt;
&lt;P&gt;That can be done with something like:&lt;/P&gt;
&lt;PRE&gt;data need;
   set have2;
   array d incorrect: ;
   do i= 1 to dim(d);
      if d[i] ne '-' then do;
         inc = d[i];
         output;
      end;
   end;
   keep correct_name inc;
run;
&lt;/PRE&gt;
&lt;P&gt;I won't claim that will work with your data, partially because you have TWO input correct_names and mostly will have the first incorrect name in the place of the correct name.&lt;/P&gt;
&lt;P&gt;Then you can match on the value of inc to the city name, use the NEED data set instead of HAVE2&lt;/P&gt;
&lt;PRE&gt;    left join need b
        on a.city_name = b.inc&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Aug 2020 19:49:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replacing-data-from-a-different-table/m-p/676589#M23723</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-08-13T19:49:00Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing data from a different table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replacing-data-from-a-different-table/m-p/676620#M23724</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/335985"&gt;@Barkat&lt;/a&gt;&amp;nbsp;wrote:
&lt;P&gt;Is there any way to use do loop for the following. Because some of the city names have about 50 incorrect names.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;Yes, here are two modified versions using loops:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. If the columns are always INCORRECT_NAME1 to INCORRECT_NAME50:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop_incorrect_names;
    proc sql;
        create table want as
        select a.name
              ,a.id
              ,a.date format MMDDYY10.
              ,case 
                   when b.correct_name is null
                       then a.city_name
                   else b.correct_name
               end as city_name
        from have1 a
        left join have2 b
            on (a.city_name = b.incorrect_name1
            %do i = 1 %to 50;     /* If the columns are always INCORRECT_NAME1 to INCORRECT_NAME50 */
                or a.city_name = b.incorrect_name&amp;amp;i
            %end;)
        ;
    quit;
%mend;
%loop_incorrect_names&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2. Or if there are not always 50 INCORRECT_NAME% columns, this will go up to the last (i.e. rightmost) INCORRECT_NAME column. (Note: I am assuming HAVE2 contains only the CORRECT_NAME column, followed by all the INCORRECT_NAME columns, ordered and named per your example.)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have2 out=meta (keep=NAME) noprint; run;

proc sql noprint; 
    select substr(name, 15) into: max_incorrect_name_number
    from meta
    having monotonic()=max(monotonic());
quit;

%put &amp;amp;=max_incorrect_name_number;

%macro loop_incorrect_names;
    proc sql;
        create table want as
        select a.name
              ,a.id
              ,a.date format MMDDYY10.
              ,case 
                   when b.correct_name is null
                       then a.city_name
                   else b.correct_name
               end as city_name
        from have1 a
        left join have2 b
            on (a.city_name = b.incorrect_name1
            %do i = 1 %to &amp;amp;max_incorrect_name_number.;    /* Using the last INCORRECT_NAME column number */
                or a.city_name = b.incorrect_name&amp;amp;i
            %end;)
        ;
    quit;
%mend;
%loop_incorrect_names&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Aug 2020 23:33:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replacing-data-from-a-different-table/m-p/676620#M23724</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2020-08-13T23:33:08Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing data from a different table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replacing-data-from-a-different-table/m-p/676644#M23726</link>
      <description>&lt;P&gt;On executing the step creating have2 i get some interesting messages in the log indicating that there should be only one variable named correct_name. So after fixing the input-statement, it seems that the problem could be solved with a format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data NameFormat;
   set have2(rename=(correct_name=Label));
   
   length Start $ 15;
   retain FmtName "$CorrectCityName";
   
   array wrong[5] incorrect_name:;
   
   do i = 1 to dim(wrong) until(wrong[i] = '-');
      Start = wrong[i];
      output;
   end;
run;

proc sort data=NameFormat nodupkey;
   by Start;
run;

proc format cntlin=NameFormat;
run;

data want;
   set have1;
   
   city_name = put(city_name, $CorrectCityName.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Aug 2020 06:31:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replacing-data-from-a-different-table/m-p/676644#M23726</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-08-14T06:31:02Z</dc:date>
    </item>
  </channel>
</rss>

