<?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: Append datasets only if they exist in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783616#M249911</link>
    <description>&lt;P&gt;I just need to append&amp;nbsp;&lt;/P&gt;&lt;P&gt;table1_202001&lt;/P&gt;&lt;P&gt;table2_202002&lt;/P&gt;&lt;P&gt;table3_202003&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;table4_202012&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but append only if they exist in database, if they don't exist, than just ignore and append the ones that exist&lt;/P&gt;</description>
    <pubDate>Thu, 02 Dec 2021 13:41:28 GMT</pubDate>
    <dc:creator>msf2021</dc:creator>
    <dc:date>2021-12-02T13:41:28Z</dc:date>
    <item>
      <title>Append datasets only if they exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783607#M249905</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have many tables with same name conventioning for every month :&lt;/P&gt;&lt;P&gt;TEMPCAAD.LIFT_&amp;amp;NOME_MODELO._&amp;amp;VERSAO_MODELO._'!! put(cur_month,yymmn6.))&lt;/P&gt;&lt;P&gt;I am trying to append all datasets but because i am doing current monht= month now and previousyearmonth= month one year ago and some datasets don't exist, how can i append them only if they exist?&lt;/P&gt;&lt;P&gt;Here is my try:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%let currentmonth = &amp;amp;anomes_scores;
%let previousyearmonth = &amp;amp;anomes_x12;


data _null_;
length string $1000;
cur_month = input("&amp;amp;previousyearmonth.01",yymmdd8.);
do until (cur_month &amp;gt; input("&amp;amp;currentmonth.01",yymmdd8.));
  string = catx(' ',trim(string),'TEMPCAAD.LIFT_&amp;amp;NOME_MODELO._&amp;amp;VERSAO_MODELO._'!! put(cur_month,yymmn6.));
  cur_month = intnx('month',cur_month,1,'b');
end;
call symput('mydatasets',trim(string));
run;

&lt;BR /&gt;%macro checkds(data);&lt;BR /&gt;%if %sysfunc(exist(data)) %then %do;&lt;BR /&gt;data WORK.LIFTS_U6M;&lt;BR /&gt;set data;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;%mend checkds;&lt;BR /&gt;%checkds(&amp;amp;mydatasets.)&lt;/PRE&gt;&lt;P&gt;The problem is that it does not return anything the macro&amp;nbsp;checkds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 13:34:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783607#M249905</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2021-12-02T13:34:19Z</dc:date>
    </item>
    <item>
      <title>Re: Append datasets only if they exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783608#M249906</link>
      <description>&lt;P&gt;What is wrong with this code? Please explain.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 13:28:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783608#M249906</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-02T13:28:54Z</dc:date>
    </item>
    <item>
      <title>Re: Append datasets only if they exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783609#M249907</link>
      <description>&lt;P&gt;&lt;SPAN&gt;The problem is that it does not return anything the macro %checkds&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 13:34:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783609#M249907</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2021-12-02T13:34:39Z</dc:date>
    </item>
    <item>
      <title>Re: Append datasets only if they exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783612#M249909</link>
      <description>&lt;P&gt;I'm not sure what you mean. What should it return? Please explain in more detail.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 13:37:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783612#M249909</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-02T13:37:33Z</dc:date>
    </item>
    <item>
      <title>Re: Append datasets only if they exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783616#M249911</link>
      <description>&lt;P&gt;I just need to append&amp;nbsp;&lt;/P&gt;&lt;P&gt;table1_202001&lt;/P&gt;&lt;P&gt;table2_202002&lt;/P&gt;&lt;P&gt;table3_202003&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;table4_202012&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but append only if they exist in database, if they don't exist, than just ignore and append the ones that exist&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 13:41:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783616#M249911</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2021-12-02T13:41:28Z</dc:date>
    </item>
    <item>
      <title>Re: Append datasets only if they exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783618#M249913</link>
      <description>&lt;P&gt;I'm really lost here, your code inside the macro does not append anything, so why are you expecting the macro to do so?&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 13:46:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783618#M249913</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-02T13:46:06Z</dc:date>
    </item>
    <item>
      <title>Re: Append datasets only if they exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783619#M249914</link>
      <description>&lt;P&gt;my previous code before i built the macro was :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;# create a list of all my datasets 
data _null_;
length string $1000;
cur_month = input("&amp;amp;previousyearmonth.01",yymmdd8.);
do until (cur_month &amp;gt; input("&amp;amp;currentmonth.01",yymmdd8.));
  string = catx(' ',trim(string),'TEMPCAAD.LIFT_&amp;amp;NOME_MODELO._&amp;amp;VERSAO_MODELO._'!! put(cur_month,yymmn6.));
  cur_month = intnx('month',cur_month,1,'b');
end;
call symput('mydatasets',trim(string));
run;

#Append my datasets (all months)
data WORK.LIFTS_U6M;
set &amp;amp;mydatasets.;
run;&lt;/PRE&gt;&lt;P&gt;but it gives me error when one of my datasets that are inside &amp;amp;mydatastets doesn't exist&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 13:52:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783619#M249914</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2021-12-02T13:52:00Z</dc:date>
    </item>
    <item>
      <title>Re: Append datasets only if they exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783622#M249917</link>
      <description>&lt;P&gt;I think you can just adjust your original code to test in the first data step, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;# create a list of all my datasets 
data _null_;
length string $1000 dsname $200;
cur_month = input("&amp;amp;previousyearmonth.01",yymmdd8.);
do until (cur_month &amp;gt; input("&amp;amp;currentmonth.01",yymmdd8.));
  dsname='TEMPCAAD.LIFT_&amp;amp;NOME_MODELO._&amp;amp;VERSAO_MODELO._'!! put(cur_month,yymmn6.)
  if exist(dsname) then
    call catx(' ',string,dsname);
  cur_month = intnx('month',cur_month,1,'b');
end;
call symput('mydatasets',trim(string));
run;

#Append my datasets (all months)
data WORK.LIFTS_U6M;
set &amp;amp;mydatasets.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Dec 2021 14:09:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783622#M249917</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2021-12-02T14:09:31Z</dc:date>
    </item>
    <item>
      <title>Re: Append datasets only if they exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783624#M249919</link>
      <description>&lt;P&gt;not really understanding this part:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;do until (cur_month &amp;gt; input("&amp;amp;currentmonth.01",yymmdd8.));
  dsname='TEMPCAAD.LIFT_&amp;amp;NOME_MODELO._&amp;amp;VERSAO_MODELO._'!! put(cur_month,yymmn6.)
  if exist(dsname) then
    call catx(' ',string,dsname);
  cur_month = intnx('month',cur_month,1,'b');
end;&lt;/PRE&gt;&lt;P&gt;the "TEMPCAAD.LIFT_&amp;amp;NOME_MODELO._&amp;amp;VERSAO_MODELO._'!! put(cur_month,yymmn6.)" is the name of the table (TEMPCAAD. is the library and the rest is the name)&amp;nbsp;&lt;/P&gt;&lt;P&gt;also, when you put&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;call catx(' ',string,dsname);&lt;/PRE&gt;&lt;P&gt;you deleted string&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 14:22:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783624#M249919</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2021-12-02T14:22:01Z</dc:date>
    </item>
    <item>
      <title>Re: Append datasets only if they exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783630#M249924</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381594"&gt;@msf2021&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;my previous code before i built the macro was :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;# create a list of all my datasets 
data _null_;
length string $1000;
cur_month = input("&amp;amp;previousyearmonth.01",yymmdd8.);
do until (cur_month &amp;gt; input("&amp;amp;currentmonth.01",yymmdd8.));
  string = catx(' ',trim(string),'TEMPCAAD.LIFT_&amp;amp;NOME_MODELO._&amp;amp;VERSAO_MODELO._'!! put(cur_month,yymmn6.));
  cur_month = intnx('month',cur_month,1,'b');
end;
call symput('mydatasets',trim(string));
run;

#Append my datasets (all months)
data WORK.LIFTS_U6M;
set &amp;amp;mydatasets.;
run;&lt;/PRE&gt;
&lt;P&gt;but it gives me error when one of my datasets that are inside &amp;amp;mydatastets doesn't exist&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You took this part out of the macro shown at the beginning of this thread:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;set &amp;amp;mydatasets.;&lt;/PRE&gt;
&lt;P&gt;so your original macro doesn't append anything.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The way to get this to work is to first write SAS code for two data sets that actually does the appending properly, without macros and without macro variables. This is a must. If you don't have working code without macros and without macro variables, then you will not get a macro to work either.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 14:37:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783630#M249924</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-02T14:37:41Z</dc:date>
    </item>
    <item>
      <title>Re: Append datasets only if they exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783702#M249971</link>
      <description>&lt;P&gt;A possible alternative may be the use of data set name list.&lt;/P&gt;
&lt;P&gt;Dummy example code:&lt;/P&gt;
&lt;PRE&gt;date want;
    set thislib.abc_&amp;amp;pdq_somename: ;
run;&lt;/PRE&gt;
&lt;P&gt;The colon at the end of the name tells SAS that you want to use &lt;STRONG&gt;all&lt;/STRONG&gt; of the data sets in the given library, in the example Thislib, whose names start with abc_&amp;amp;pdq_somename.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warning: this is not efficient code for large data sets as it does process each record like any data step code but does not require any step to check for existence of the sets.&lt;/P&gt;
&lt;P&gt;If your data sets are not consistent about variable types or lengths you may get warnings about such so you do want to read the log carefully and address those issues.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your initial posted code doesn't work because 1) you did not use the macro parameter data correctly, references require &amp;amp;data and 2) the Exist function would only work with a single data set name and you are attempting to pass a space delimited list.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 17:18:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783702#M249971</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-12-02T17:18:22Z</dc:date>
    </item>
    <item>
      <title>Re: Append datasets only if they exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783703#M249972</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I like this approach (since it is what I would have suggested), but you need a little clean-up here:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;dsname='TEMPCAAD.LIFT_&amp;amp;NOME_MODELO._&amp;amp;VERSAO_MODELO._'!! put(cur_month,yymmn6.)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Besides missing a semicolon at the end of the statement, you need to switch from single quotes to double quotes, to allow the macro variables to resolve.&amp;nbsp; Also, for clarity, I might suggest:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if exist(dsname) then string = catx(' ', string, dsname);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 17:26:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783703#M249972</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-12-02T17:26:25Z</dc:date>
    </item>
    <item>
      <title>Re: Append datasets only if they exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783860#M250049</link>
      <description>&lt;P&gt;You are right about the missing semicolon and the double quotes - although the code (kind of) worked in the original version, because the macro variables would be resolved in the SET statement later. But when calling the EXIST function they do not get resolved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason that I normally use CALL CATX instead of a function call (when appending to the same variable) is that it is faster.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The assignment&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;string = catx(' ', string, dsname);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;does something like this:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Moves the contents of STRING to a temporary buffer&lt;/LI&gt;
&lt;LI&gt;Performs CALL CATX(' ',&amp;lt;buffer&amp;gt;,dsname)&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Moves the result back to STRING&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;The CALL CATX operation just performs step 3. So it both saves CPU time and memory. And once you have learned the syntax of CALL CATX, I do not see any difference in the "clarity" of the statement. It is just that most people use the function all the time, meaning that they are not so used to the CALL syntax. But it is fully documented, and not hard to understand.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Dec 2021 08:33:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-datasets-only-if-they-exist/m-p/783860#M250049</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2021-12-03T08:33:04Z</dc:date>
    </item>
  </channel>
</rss>

