<?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: Macro to append a string to a list conditionally in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-append-a-string-to-a-list-conditionally/m-p/970951#M377207</link>
    <description>Thanks ! Works perfect now. Its annoying when the solution is so simple &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
    <pubDate>Thu, 17 Jul 2025 02:55:22 GMT</pubDate>
    <dc:creator>simmwa</dc:creator>
    <dc:date>2025-07-17T02:55:22Z</dc:date>
    <item>
      <title>Macro to append a string to a list conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-append-a-string-to-a-list-conditionally/m-p/970840#M377183</link>
      <description>&lt;P&gt;Trying to create a list of datasets that exist, so I can use that list in a data step to create a combined dataset.&lt;/P&gt;&lt;P&gt;Below code is the best I can do. Can someone show me how to do this ?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro setvars;
	%global mylist = '';

    %if %sysfunc(exist(MYRAW.&amp;amp;mysid.MBS1)) %then
		mylist = cats(mylist,VARS_MBS1);
	%if %sysfunc(exist(MYRAW.&amp;amp;mysid.DWS1)) %then
		mylist = cats(mylist,VARS_DWS1);
	%if %sysfunc(exist(MYRAW.&amp;amp;mysid.FMS1)) %then
		mylist = cats(mylist,VARS_FMS1);
	%if %sysfunc(exist(MYRAW.&amp;amp;mysid.PSS1)) %then
		mylist = cats(mylist,VARS_PSS1);
%mend setvars;

%setvars;

data work.all_vars_1;
set &amp;amp;mylist.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Jul 2025 05:37:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-append-a-string-to-a-list-conditionally/m-p/970840#M377183</guid>
      <dc:creator>simmwa</dc:creator>
      <dc:date>2025-07-16T05:37:46Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to append a string to a list conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-append-a-string-to-a-list-conditionally/m-p/970849#M377188</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to concatenate text in a macro you can use a &lt;FONT face="courier new,courier"&gt;%let&lt;/FONT&gt; statement to assign the new value and then place the new text you want next to the existing text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;E.g., in place of:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;mylist = cats(mylist,VARS_PSS1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;you can use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mylist = &amp;amp;mylist VARS_PSS1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and then similarly for the other assignments.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this does not work then please also share the log using the "&amp;lt;/&amp;gt;" icon, including the code and any messages - especially errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jul 2025 08:37:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-append-a-string-to-a-list-conditionally/m-p/970849#M377188</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2025-07-16T08:37:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to append a string to a list conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-append-a-string-to-a-list-conditionally/m-p/970851#M377190</link>
      <description>&lt;P&gt;It looks (from the code) that you are looking 4 particular data sets in one library, why not to use the power of dictionary.tables?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select catx(".",libname,libname)
  into :myList separated by " "
  from dictionary.tables
  where libname='MYRAW'
  and memname in (
    "&amp;amp;mysid.MBS1"
   ,"&amp;amp;mysid.DWS1"
   ,"&amp;amp;mysid.FMS1"
   ,"&amp;amp;mysid.PSS1"
  )
  ;
quit;

data want;
  set &amp;amp;myList.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jul 2025 08:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-append-a-string-to-a-list-conditionally/m-p/970851#M377190</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-07-16T08:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to append a string to a list conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-append-a-string-to-a-list-conditionally/m-p/970866#M377194</link>
      <description>&lt;P&gt;To concatenate text strings in macro code just write the text string next to each other.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example to concatenate B to A just type AB.&amp;nbsp; So to append dataset5 to the value of mylist just type it next to the value of mylist.&amp;nbsp; Like this:&amp;nbsp; &lt;FONT face="courier new,courier"&gt;&amp;amp;mylist dataset5&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro setvars;
%if not %symexist(mylist) %then %global mylist;
%let mylist=;
%if %sysfunc(exist(MYRAW.&amp;amp;mysid.MBS1)) %then %let mylist = &amp;amp;mylist MYRAW.&amp;amp;mysid.MBS1;
%if %sysfunc(exist(MYRAW.&amp;amp;mysid.DWS1)) %then %let mylist = &amp;amp;mylist MYRAW.&amp;amp;mysid.DWS1;
%if %sysfunc(exist(MYRAW.&amp;amp;mysid.FMS1)) %then %let mylist = &amp;amp;mylist MYRAW.&amp;amp;mysid.FMS1;
%if %sysfunc(exist(MYRAW.&amp;amp;mysid.PSS1)) %then %let mylist = &amp;amp;mylist MYRAW.&amp;amp;mysid.PSS1;
%mend setvars;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For you actual problem perhaps you want the macro to just emit the names directly instead of building a macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro datsets(list);
%local i ds ;
%do i=1 %to %sysfunc(countw(&amp;amp;list,%str( )));
  %let ds=%scan(&amp;amp;list,&amp;amp;i,%str( ));
  %if %sysfunc(exist(&amp;amp;ds)) %then &amp;amp;ds ;
%end;
%mend datasets;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you could use it like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.all_vars_1;
  set %datasets(MYRAW.&amp;amp;mysid.MBS1 MYRAW.&amp;amp;mysid.DWS1 MYRAW.&amp;amp;mysid.FMS1 MYRAW.&amp;amp;mysid.PSS1);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Jul 2025 15:16:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-append-a-string-to-a-list-conditionally/m-p/970866#M377194</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-07-16T15:16:13Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to append a string to a list conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-append-a-string-to-a-list-conditionally/m-p/970951#M377207</link>
      <description>Thanks ! Works perfect now. Its annoying when the solution is so simple &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Thu, 17 Jul 2025 02:55:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-append-a-string-to-a-list-conditionally/m-p/970951#M377207</guid>
      <dc:creator>simmwa</dc:creator>
      <dc:date>2025-07-17T02:55:22Z</dc:date>
    </item>
  </channel>
</rss>

