<?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: Merge data sets that some of them doesnt exist in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-that-some-of-them-doesnt-exist/m-p/684947#M207668</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the case where no dataset exist is possible, you can try the following :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro merge_revs(start, end);

    data _NULL_;
        length datastep $100.;
        datastep='data want; merge';
        do year=&amp;amp;end. to &amp;amp;start. by -1;
            dsname=cats('Rev', year);
            if exist(dsname) then do;
                ok=1;
                call catx(' ', datastep, dsname);
            end;
        end;

    if ok then call execute(cats(datastep,'; by id; run;'));
    else put "No dataset for the given time range";
    run;

%mend;

%merge_revs(2017,  2020)

%merge_revs(2014, 2017)

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 18 Sep 2020 12:22:01 GMT</pubDate>
    <dc:creator>gamotte</dc:creator>
    <dc:date>2020-09-18T12:22:01Z</dc:date>
    <item>
      <title>Merge data sets that some of them doesnt exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-that-some-of-them-doesnt-exist/m-p/684927#M207658</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Let's say that in my work I receive a list of data sets from my manager and he ask me to Join them (Full join) By customer ID.&lt;/P&gt;
&lt;P&gt;Let's say that some of the data sets in the list are not existing (manager made mistake and sent some data sets that are not existing).&lt;/P&gt;
&lt;P&gt;One way to work is to check &amp;nbsp;if each data set exist and then merge the data sets that exists .&lt;/P&gt;
&lt;P&gt;I am looking for another solution that merge the all data sets and if one of the data sets doesn't exist then I will not get error.&lt;/P&gt;
&lt;P&gt;What is the way to do it please?&lt;/P&gt;
&lt;P&gt;Please find example&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let  DatasetsList=Rev2019 Rev2018 Rev2017;
/*List of data sets that my manager sent me */
/*In real world it contains 50 data sets*/

Data Rev2019 ;
input ID Rev;
cards;
1 10
2 20
;
run;

Data Rev2018 ;
input ID Rev;
cards;
1 15
2 25
;
run;

Data wanted;
merge %list;
by ID;
Run;
/*data set Rev2018  doesn't exist so I get error*/


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 10:36:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-that-some-of-them-doesnt-exist/m-p/684927#M207658</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-09-18T10:36:29Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data sets that some of them doesnt exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-that-some-of-them-doesnt-exist/m-p/684929#M207660</link>
      <description>&lt;P&gt;You would need a macro loop to test if each data set in &amp;amp;datasetslist exists.&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;%let DatasetsList=Rev2019 Rev2018 Rev2017;

%macro dothis;
data wanted;
    merge 
        %do i=1 %to %sysfunc(countw(&amp;amp;datasetslist));
             %let this_ds=%scan(&amp;amp;datasetslist,&amp;amp;i,%str( ));
             %if %sysfunc(exist(&amp;amp;this_ds)) %then &amp;amp;this_ds;
        %end;
    ;
    by id;
run;
%mend;
%dothis    &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This code probably still fails if ALL of the data sets do not exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 11:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-that-some-of-them-doesnt-exist/m-p/684929#M207660</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-18T11:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data sets that some of them doesnt exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-that-some-of-them-doesnt-exist/m-p/684943#M207666</link>
      <description>&lt;P&gt;Why do have to use a macro-variable? Are there datasets in the library whose name also begins with "rev"? If not you could use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge work.Rev:;
  by Id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Sep 2020 12:05:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-that-some-of-them-doesnt-exist/m-p/684943#M207666</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-09-18T12:05:04Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data sets that some of them doesnt exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-that-some-of-them-doesnt-exist/m-p/684947#M207668</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the case where no dataset exist is possible, you can try the following :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro merge_revs(start, end);

    data _NULL_;
        length datastep $100.;
        datastep='data want; merge';
        do year=&amp;amp;end. to &amp;amp;start. by -1;
            dsname=cats('Rev', year);
            if exist(dsname) then do;
                ok=1;
                call catx(' ', datastep, dsname);
            end;
        end;

    if ok then call execute(cats(datastep,'; by id; run;'));
    else put "No dataset for the given time range";
    run;

%mend;

%merge_revs(2017,  2020)

%merge_revs(2014, 2017)

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Sep 2020 12:22:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-that-some-of-them-doesnt-exist/m-p/684947#M207668</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-09-18T12:22:01Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data sets that some of them doesnt exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-that-some-of-them-doesnt-exist/m-p/684983#M207685</link>
      <description>&lt;P&gt;If no dataset in &amp;amp;Datasetslist exists, the merge instruction will be equivalent to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set &amp;amp;syslast.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;that is, the wanted dataset will be a copy of the last processed dataset if it contains a column "id"&lt;/P&gt;
&lt;P&gt;or, if not, the program will raise an error.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 14:25:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-that-some-of-them-doesnt-exist/m-p/684983#M207685</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-09-18T14:25:38Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data sets that some of them doesnt exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-that-some-of-them-doesnt-exist/m-p/685034#M207706</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Why do have to use a macro-variable? Are there datasets in the library whose name also begins with "rev"? If not you could use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge work.Rev:;
  by Id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, in the case where the data sets of interest constitute all data sets whose name begins with Rev, this is much easier.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am usually unwilling to make such an assumption about someone else's data, but certainly the original poster could know if such an assumption was warranted or not.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 16:53:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-that-some-of-them-doesnt-exist/m-p/685034#M207706</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-18T16:53:37Z</dc:date>
    </item>
  </channel>
</rss>

