<?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 surround a list of values with a single quote in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/501128#M133582</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;data want (keep=word);
  do i=1 to countw(string_from_excel," ");
    word=scan(string_from_excel,i," ");
    output;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;Whilst not knowing how the string is got, i.e. if its an import just read it into a dataste there, its pretty simple to break it out into data, this may however be overkill for 1 or 2 parameters, but in that case just change the source.&lt;/P&gt;
&lt;P&gt;I agree of course that sometimes there are restrictions, but how many times are those restrictions self imposed?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes you could type this code every time you encounter this situation to get the string values into a data step; or you could simply, one time, create a macro to do get the end result you want (as I have done) and avoid the data step portion, and then use %put_quotes_around(&amp;amp;macrovariablename) ... which is easier to remember, and in my opinion, more readable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, yes this is data, it is string data, so data steps will do a fine job on it; and macros work well on string data&amp;nbsp;in this case.&lt;/P&gt;</description>
    <pubDate>Wed, 03 Oct 2018 13:26:41 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2018-10-03T13:26:41Z</dc:date>
    <item>
      <title>Macro to surround a list of values with a single quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/500718#M133380</link>
      <description>&lt;P&gt;Hi I have a variable being passed from excel which is&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;x=CIRT201808, CIRT201608, 18DI01;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to surround each value with a single quote then use a macro to say&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if z in ('CIRT201808', 'CIRT201608', '18DI01') then do something.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 13:04:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/500718#M133380</guid>
      <dc:creator>rramcharan</dc:creator>
      <dc:date>2018-10-02T13:04:57Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to surround a list of values with a single quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/500720#M133381</link>
      <description>&lt;P&gt;This works on a string that has the variables separated by spaces and no commas (which you can create using the TRANSLATE function if your original text string has the commas). Alternatively, the macro can be modified to handle the commas, but I didn't do that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro put_quotes_around(string);
	%let aggregate=;
	%do i=1 %to %sysfunc(countw(&amp;amp;string));
		%let thisword=%scan(&amp;amp;string,&amp;amp;i,%str( ));
		%let thiswordq=%nrstr(%')&amp;amp;thisword%nrstr(%');
		%if &amp;amp;i &amp;lt; %sysfunc(countw(&amp;amp;string)) %then %let comma=%str(,); %else %let comma=;
		%let aggregate=&amp;amp;aggregate &amp;amp;thiswordq &amp;amp;comma;
	%end;
	%put &amp;amp;=aggregate;
%mend;
%put_quotes_around(CIRT201808 CIRT201608 18DI01)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 13:21:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/500720#M133381</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-10-02T13:21:24Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to surround a list of values with a single quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/500721#M133382</link>
      <description>&lt;P&gt;excellent thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 13:19:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/500721#M133382</guid>
      <dc:creator>rramcharan</dc:creator>
      <dc:date>2018-10-02T13:19:15Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to surround a list of values with a single quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/500729#M133386</link>
      <description>&lt;P&gt;I should add, for completeness, to use this macro for example, in PROC SQL, comment out the %PUT statement near the end, then add a line that just says &lt;FONT face="courier new,courier"&gt;&amp;amp;aggregate&lt;/FONT&gt;&amp;nbsp;as the last line of the macro just above the &lt;FONT face="courier new,courier"&gt;%mend;&lt;/FONT&gt; command, then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
      something something something ...
      where variable in (%put_quotes_around(CIRT2018080 CIRT201608 18DI01));
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 13:38:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/500729#M133386</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-10-02T13:38:57Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to surround a list of values with a single quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/500746#M133388</link>
      <description>&lt;P&gt;How do you "get a variable passed from Excel"?&amp;nbsp; If you have data, then store it in a dataset, then use the dataset, it is far simpler, easier coding, and more robust to do:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table want as
  select * 
  from   have
  where z in (select param from paramdataset);
quit;&lt;/PRE&gt;
&lt;P&gt;So paramdataset would look like:&lt;BR /&gt;Param&lt;/P&gt;
&lt;P&gt;CIRT201808&lt;/P&gt;
&lt;P&gt;CIRT201608&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The above dataset is expandable far beyond the range of macro variables, and is far simpler to work with.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 14:04:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/500746#M133388</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-10-02T14:04:26Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to surround a list of values with a single quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/500800#M133402</link>
      <description>&lt;P&gt;Suppose he gets a string from Excel. The string is:&amp;nbsp;&lt;SPAN&gt;CIRT201808, CIRT201608, 18DI01 (with or without the commas)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I realize it's not an ideal way to do things, but sometimes you don't get to choose the format of the data because others are creating the Excel file and you have no control over it.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 15:12:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/500800#M133402</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-10-02T15:12:03Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to surround a list of values with a single quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/501044#M133542</link>
      <description>&lt;PRE&gt;data want (keep=word);
  do i=1 to countw(string_from_excel," ");
    word=scan(string_from_excel,i," ");
    output;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;Whilst not knowing how the string is got, i.e. if its an import just read it into a dataste there, its pretty simple to break it out into data, this may however be overkill for 1 or 2 parameters, but in that case just change the source.&lt;/P&gt;
&lt;P&gt;I agree of course that sometimes there are restrictions, but how many times are those restrictions self imposed?&lt;/P&gt;</description>
      <pubDate>Wed, 03 Oct 2018 07:51:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/501044#M133542</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-10-03T07:51:33Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to surround a list of values with a single quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/501111#M133574</link>
      <description>&lt;P&gt;&lt;SPAN&gt;if z in ('CIRT201808', 'CIRT201608', '18DI01') then do something.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;--&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;if find(symget('x'),strip(z)) then do something.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Oct 2018 12:49:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/501111#M133574</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-10-03T12:49:56Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to surround a list of values with a single quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/501117#M133577</link>
      <description>&lt;P&gt;&lt;SPAN&gt;if z in ('CIRT201808', 'CIRT201608', '18DI01') then do something.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;--&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;if find(symget('x'),strip(z)) then do something.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Oct 2018 12:57:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/501117#M133577</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-10-03T12:57:59Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to surround a list of values with a single quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/501128#M133582</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;data want (keep=word);
  do i=1 to countw(string_from_excel," ");
    word=scan(string_from_excel,i," ");
    output;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;Whilst not knowing how the string is got, i.e. if its an import just read it into a dataste there, its pretty simple to break it out into data, this may however be overkill for 1 or 2 parameters, but in that case just change the source.&lt;/P&gt;
&lt;P&gt;I agree of course that sometimes there are restrictions, but how many times are those restrictions self imposed?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes you could type this code every time you encounter this situation to get the string values into a data step; or you could simply, one time, create a macro to do get the end result you want (as I have done) and avoid the data step portion, and then use %put_quotes_around(&amp;amp;macrovariablename) ... which is easier to remember, and in my opinion, more readable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, yes this is data, it is string data, so data steps will do a fine job on it; and macros work well on string data&amp;nbsp;in this case.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Oct 2018 13:26:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-surround-a-list-of-values-with-a-single-quote/m-p/501128#M133582</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-10-03T13:26:41Z</dc:date>
    </item>
  </channel>
</rss>

