<?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: generating lists out of a macro invocation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/379468#M91345</link>
    <description>&lt;P&gt;A less generic macro relying on 1) space delimited list of words and 2) only expected to provide quotes:&lt;/P&gt;
&lt;P&gt;I have a %put just to display the resolved version because during development I seldom have a place to use the resolved value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro stlist(vars);

  %let words = %sysfunc(countw(&amp;amp;vars));

  %let res=;
  %if &amp;amp;words &amp;gt; 0 %then %do i = 1 %to &amp;amp;words;
      %let res= &amp;amp;res %sysfunc(quote(%scan(&amp;amp;vars,&amp;amp;i)));
  %end;
   %put &amp;amp;res;
%mend stlist;&lt;/PRE&gt;</description>
    <pubDate>Wed, 26 Jul 2017 17:08:07 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-07-26T17:08:07Z</dc:date>
    <item>
      <title>generating lists out of a macro invocation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/379460#M91342</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm chasing my tail on what should be a simple macro and getting nowhere. &amp;nbsp;Given the following code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro stlist(vars,q=%str( ), c=%str( ));&lt;/P&gt;
&lt;P&gt;&amp;nbsp; %let i=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; %do %while (%scan(&amp;amp;vars,&amp;amp;i) ^= );&lt;BR /&gt;&amp;nbsp; %let sv = %scan(&amp;amp;vars,&amp;amp;i);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; %if &amp;amp;i ^= 1 %then &amp;amp;c;&lt;BR /&gt;&amp;nbsp; %let res=&amp;amp;q.&amp;amp;sv.&amp;amp;q;&lt;BR /&gt;&amp;amp;res&lt;BR /&gt;&amp;nbsp; %let i= %eval(&amp;amp;i+1);&lt;BR /&gt;&amp;nbsp; %end;&lt;/P&gt;
&lt;P&gt;%mend stlist;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let tvars=cat dog pig;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;* Goal: produce the following statement...&lt;BR /&gt; array t(3) $ a b c ('cat' 'dog' 'pig');&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;array t(3) $ a b c ( %stlist(&amp;amp;tvars, q=%str(%')) );&lt;BR /&gt; r="%stlist(&amp;amp;tvars, q=%str(%'))";&lt;BR /&gt; put r=;&lt;BR /&gt; stop;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I run this as-is, SAS throws a syntax error. &amp;nbsp;If I comment out the array statement and let the result of the macro invocation be assigned to the variable r, it give me what I want for the intial values for a b and c in the array statement. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What am I missing? &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;--Ben&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jul 2017 16:39:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/379460#M91342</guid>
      <dc:creator>BenConner</dc:creator>
      <dc:date>2017-07-26T16:39:33Z</dc:date>
    </item>
    <item>
      <title>Re: generating lists out of a macro invocation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/379461#M91343</link>
      <description>&lt;P&gt;Where do you originally get the information from?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it's a table when you select it you can use the QUOTE function. Otherwise, I'd probably look at the macro here to turn my list into a quoted list:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.datasavantconsulting.com/roland/Spectre/utilmacros/quotelst.sas" target="_blank"&gt;http://www.datasavantconsulting.com/roland/Spectre/utilmacros/quotelst.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro quotelst(str,quote=%str(%"),delim=%str( ));
  %local i quotelst;
  %let i=1;
  %do %while(%length(%qscan(&amp;amp;str,&amp;amp;i,%str( ))) GT 0);
    %if %length(&amp;amp;quotelst) EQ 0 %then %let quotelst=&amp;amp;quote.%qscan(&amp;amp;str,&amp;amp;i,%str( ))&amp;amp;quote;
    %else %let quotelst=&amp;amp;quotelst.&amp;amp;quote.%qscan(&amp;amp;str,&amp;amp;i,%str( ))&amp;amp;quote;
    %let i=%eval(&amp;amp;i + 1);
    %if %length(%qscan(&amp;amp;str,&amp;amp;i,%str( ))) GT 0 %then %let quotelst=&amp;amp;quotelst.&amp;amp;delim;
  %end;
%unquote(&amp;amp;quotelst)
%mend quotelst;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jul 2017 16:43:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/379461#M91343</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-26T16:43:16Z</dc:date>
    </item>
    <item>
      <title>Re: generating lists out of a macro invocation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/379468#M91345</link>
      <description>&lt;P&gt;A less generic macro relying on 1) space delimited list of words and 2) only expected to provide quotes:&lt;/P&gt;
&lt;P&gt;I have a %put just to display the resolved version because during development I seldom have a place to use the resolved value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro stlist(vars);

  %let words = %sysfunc(countw(&amp;amp;vars));

  %let res=;
  %if &amp;amp;words &amp;gt; 0 %then %do i = 1 %to &amp;amp;words;
      %let res= &amp;amp;res %sysfunc(quote(%scan(&amp;amp;vars,&amp;amp;i)));
  %end;
   %put &amp;amp;res;
%mend stlist;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jul 2017 17:08:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/379468#M91345</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-07-26T17:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: generating lists out of a macro invocation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/379471#M91348</link>
      <description>&lt;P&gt;First note, this particular application doesn't require single quotes. &amp;nbsp;Double quotes would work just fine. &amp;nbsp;For that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro stlist (list=);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;%local i;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;%do i=1 %to %sysfunc(countw(&amp;amp;list));&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; "%scan(&amp;amp;list, &amp;amp;i)"&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;%end;&lt;/P&gt;
&lt;P&gt;%mend stlist;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you really need single quotes, you can replace the interior statement with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%unquote(&lt;FONT color="#FF0000"&gt;%str(&lt;/FONT&gt;%'%scan(&amp;amp;list,&amp;amp;i)%'&lt;FONT color="#FF0000"&gt;)&lt;/FONT&gt;)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Calling the macro:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;%stlist (cat dog pig)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;You can use this anywhere you want the quoted text to appear. &amp;nbsp;That could be inside the ARRAY statement, but it could also be here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let r = %stlist(cat dog pig);&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jul 2017 19:11:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/379471#M91348</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-07-26T19:11:50Z</dc:date>
    </item>
    <item>
      <title>Re: generating lists out of a macro invocation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/379476#M91351</link>
      <description>&lt;P&gt;The list will be handed to the macro invocation either hard-coded or as a macro field. &amp;nbsp;The input list&amp;nbsp;actually is a list of SAS variable names. &amp;nbsp;Some SAS statements need the names quoted, others comma separated, and still others with both commas and quotes. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I just found that using %cmpres(&amp;amp;res) &amp;nbsp; rather than &amp;amp;res &amp;nbsp; by itself cleared it up. &amp;nbsp;Will check out the other approaches for a more elegant solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;--Ben&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jul 2017 17:21:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/379476#M91351</guid>
      <dc:creator>BenConner</dc:creator>
      <dc:date>2017-07-26T17:21:25Z</dc:date>
    </item>
    <item>
      <title>Re: generating lists out of a macro invocation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/379485#M91357</link>
      <description>&lt;P&gt;Your macro is way over complicated and is missing %LOCAL statement for I SV and RES.&lt;/P&gt;
&lt;P&gt;But &lt;STRONG&gt;the real problem&lt;/STRONG&gt; is that you are generating macro quoted strings that are then causing SAS to not understand what code you wanted.&amp;nbsp;You could probably remove the errors by just using %UNQUOTE() macro function to remove the macro quoting that you added.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array t(3) $ a b c ( %unquote(%stlist(&amp;amp;tvars, q=%str(%'))) );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is simple code to take a space delimited list of words (without any embedded quote characters) and return a quoted list.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;"%sysfunc(tranwrd(&amp;amp;tvars,%str( )," "))"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now this will not work if you list has more than one space between the words, but you can use COMPBL() to fix that. &amp;nbsp;So if the list of variables is user entered then use this line to clean it up to one space between variable name.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let tvars=%sysfunc(compbl(&amp;amp;tvars));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So here is a little example including using the list to generate initial values for an array ;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let tvars=cat   dog    fox;
%let tvars=%sysfunc(compbl(&amp;amp;tvars));
%let n=%sysfunc(countw(&amp;amp;tvars));
data _null_;
  array t (&amp;amp;n) $32 _temporary_ ("%sysfunc(tranwrd(&amp;amp;tvars,%str( )," "))");
  xx = catx(' ',of t(*));
  put xx=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;xx=cat dog fox
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;If you want a flexible tool for generating lists in different forms use this %QLIST() macro.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/sasutils/macros/blob/master/qlist.sas" target="_blank"&gt;https://github.com/sasutils/macros/blob/master/qlist.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Here are some examples&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let tlist=dog  cat fox;
%put %qlist(&amp;amp;tlist);
%put %qlist(&amp;amp;tlist,paren=0);
%put %qlist(&amp;amp;tlist,quote=2);
%put %qlist(&amp;amp;tlist,comma=0);
%put %qlist(A  B,dsd=1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;124  %let tlist=dog  cat fox;
125  %put %qlist(&amp;amp;tlist);
('dog','cat','fox')
126  %put %qlist(&amp;amp;tlist,paren=0);
'dog','cat','fox'
127  %put %qlist(&amp;amp;tlist,quote=2);
("dog","cat","fox")
128  %put %qlist(&amp;amp;tlist,comma=0);
('dog' 'cat' 'fox')
129  %put %qlist(A  B,dsd=1);
('A','','B')
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jul 2017 17:54:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/379485#M91357</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-26T17:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: generating lists out of a macro invocation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/379493#M91361</link>
      <description>&lt;P&gt;Just plugging my favorite list manipulation macro, Richard DeVenezia's %seplist:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.devenezia.com/downloads/sas/macros/index.php?m=seplist" target="_blank"&gt;http://www.devenezia.com/downloads/sas/macros/index.php?m=seplist&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I added an %UNQUOTE to the last line of the macro definition, i.e.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%unquote(&amp;amp;emit)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It allows you to add/remove/modify delimiters as well as prefixes/suffixes and also nest items&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Useful for simple list manipulation:&lt;/P&gt;
&lt;PRE&gt;128  %let tlist= dog cat fox;
129
130  %put %seplist(&amp;amp;tlist);
dog,cat,fox
131
132  %put %seplist(&amp;amp;tlist,nest=Q);
'dog','cat','fox'
133
134  %put %seplist(&amp;amp;tlist,nest=QQ);
"dog","cat","fox"
135
136  %put %seplist(&amp;amp;tlist,nest=QQ,dlm=%str( ));
"dog" "cat" "fox"
&lt;/PRE&gt;
&lt;P&gt;And can even&amp;nbsp;generate blocks of code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;139  %seplist(shoes class prdsale
140          ,prefix=proc print data=sashelp.
141          ,suffix=%str((obs=5);run;)
142          ,dlm=%str( )
143          )
MPRINT(SEPLIST):   proc print data=sashelp.shoes(obs=5);
MPRINT(SEPLIST):  run;

NOTE: There were 5 observations read from the data set SASHELP.SHOES.

MPRINT(SEPLIST):   proc print data=sashelp.class(obs=5);
MPRINT(SEPLIST):  run;

NOTE: There were 5 observations read from the data set SASHELP.CLASS.

MPRINT(SEPLIST):   proc print data=sashelp.prdsale(obs=5);
MPRINT(SEPLIST):  run;

NOTE: There were 5 observations read from the data set SASHELP.PRDSALE.
&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jul 2017 18:12:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/379493#M91361</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-07-26T18:12:03Z</dc:date>
    </item>
    <item>
      <title>Re: generating lists out of a macro invocation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/380690#M91413</link>
      <description>&lt;P&gt;Wow.&amp;nbsp; Obviously this has been considered before. &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&amp;nbsp; I don't even know where to begin.&amp;nbsp; Thanks to all for pointing out what I had overlooked, and especially for sharing better code!&amp;nbsp; Careful thought beats quick and dirty every time...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Greatly appreciated!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;--Ben&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2017 14:31:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/380690#M91413</guid>
      <dc:creator>BenConner</dc:creator>
      <dc:date>2017-07-27T14:31:53Z</dc:date>
    </item>
    <item>
      <title>Re: generating lists out of a macro invocation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/381414#M91424</link>
      <description>&lt;P&gt;I have written a macro named calltext that can handle this problem;&lt;/P&gt;&lt;P&gt;your problem required a hard-coded (typed in) list.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;macro calltext reads a list, a control data set and provides the desired string from the values in the data set&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.sascommunity.org/wiki/Macro_CallText" target="_blank"&gt;http://www.sascommunity.org/wiki/Macro_CallText&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ron Fehd&amp;nbsp; list processing maven&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;see also macro Call-Macro, which returns a macro call from the values in the data set&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.sascommunity.org/wiki/Macro_CallMacr" target="_blank"&gt;http://www.sascommunity.org/wiki/Macro_CallMacr&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2017 15:36:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-lists-out-of-a-macro-invocation/m-p/381414#M91424</guid>
      <dc:creator>Ron_MacroMaven</dc:creator>
      <dc:date>2017-07-27T15:36:45Z</dc:date>
    </item>
  </channel>
</rss>

