<?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: How to handle spaces in a macro variable list in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524632#M142682</link>
    <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;!</description>
    <pubDate>Fri, 04 Jan 2019 17:52:31 GMT</pubDate>
    <dc:creator>lstockman</dc:creator>
    <dc:date>2019-01-04T17:52:31Z</dc:date>
    <item>
      <title>How to handle spaces in a macro variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524406#M142605</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a list of counties that have spaces in their names. Rather than change all the spaces to underscores or remove them, is there a way I can format the list so the macro will run with the original value?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code I am trying to pass the macro 'list'&amp;nbsp;through to create a file for each county in my list.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loopit(lhjlist);
   %let n = %sysfunc(countw(&amp;amp;lhjlist));
   %do I=1 %to &amp;amp;n;
      %let val = %scan(&amp;amp;lhjlist,&amp;amp;I);

      data want_&amp;amp;val;
         set have;
         where county= "&amp;amp;val";
      run;

	%end;
%mend;

%let list= 
LOS ANGELES
CONTRA COSTA
SAN DIEGO
;

%loopit(&amp;amp;list);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jan 2019 22:50:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524406#M142605</guid>
      <dc:creator>lstockman</dc:creator>
      <dc:date>2019-01-03T22:50:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle spaces in a macro variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524410#M142608</link>
      <description>&lt;P&gt;Put some character between the county names, such as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list=LOS ANGELES~CONTRA COSTA~SAN DIEGO;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n = %sysfunc(countw(&amp;amp;lhjlist,~));
%do I=1 %to &amp;amp;n;
      %let val = %scan(&amp;amp;lhjlist,&amp;amp;I,~);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jan 2019 22:56:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524410#M142608</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-01-03T22:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle spaces in a macro variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524411#M142609</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;'s recommendation is neat.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;May I know your bigger objective or is it just splitting datasets as your code suggests?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jan 2019 23:06:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524411#M142609</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-03T23:06:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle spaces in a macro variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524412#M142610</link>
      <description>&lt;P&gt;Thanks, that works for some things and is very helpful to know. In my code below, the where statement is working but the output dataset is only the first word of the county i.e. work.want_los and work.want_san. Makes sense as I don't think an output dataset can have a space (?) Ultimately, I want to output a .csv or .xls file for each county in my list. Can I do that without first making individual datasets for each?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jan 2019 23:29:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524412#M142610</guid>
      <dc:creator>lstockman</dc:creator>
      <dc:date>2019-01-03T23:29:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle spaces in a macro variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524454#M142625</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/162166"&gt;@lstockman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks, that works for some things and is very helpful to know. In my code below, the where statement is working but the output dataset is only the first word of the county i.e. work.want_los and work.want_san. Makes sense as I don't think an output dataset can have a space (?) Ultimately, I want to output a .csv or .xls file for each county in my list. Can I do that without first making individual datasets for each?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You don't have to split data to create file for each county.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Look at &lt;A href="http://support.sas.com/kb/24/599.html" target="_blank"&gt;http://support.sas.com/kb/24/599.html&lt;/A&gt; for an example.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jan 2019 06:51:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524454#M142625</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-01-04T06:51:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle spaces in a macro variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524459#M142629</link>
      <description>&lt;P&gt;First, sort by county.&lt;/P&gt;
&lt;P&gt;Then do&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set have;
by county;
length fname $200;
if first.county then fname = "&amp;amp;path./" !! strip(county) !! '.csv';
file dummy dlm=',' dsd filevar=fname;
put .....;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For easier handling of the csv files, consider replacing the blanks in county with underlines, and convert everything to lowercase.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jan 2019 09:06:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524459#M142629</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-04T09:06:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle spaces in a macro variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524476#M142635</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/162166"&gt;@lstockman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks, that works for some things and is very helpful to know. In my code below, the where statement is working but the output dataset is only the first word of the county i.e. work.want_los and work.want_san. Makes sense as I don't think an output dataset can have a space (?) Ultimately, I want to output a .csv or .xls file for each county in my list. Can I do that without first making individual datasets for each?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, you can do this without creating a SAS data set, and your .csv or .xls files CAN have space(s) in the file name. I believe Kurt Bremser has provided a solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also replace the space with an underscore in the data set names. This is probably unnecessary extra work given Kurt Bremser's solution, but for completeness, this gives you legal SAS data set names. Naturally, it becomes a pain to have data set names with underscores and corresponding data values that have spaces.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want_%sysfunc(translate(&amp;amp;val,_,%str( )));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jan 2019 13:00:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524476#M142635</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-01-04T13:00:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle spaces in a macro variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524631#M142681</link>
      <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; ! This works and is so much faster! Do you know if there is a statement I can add to retain the variable names as the first row in the resulting csv files?</description>
      <pubDate>Fri, 04 Jan 2019 17:51:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524631#M142681</guid>
      <dc:creator>lstockman</dc:creator>
      <dc:date>2019-01-04T17:51:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle spaces in a macro variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524632#M142682</link>
      <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;!</description>
      <pubDate>Fri, 04 Jan 2019 17:52:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524632#M142682</guid>
      <dc:creator>lstockman</dc:creator>
      <dc:date>2019-01-04T17:52:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle spaces in a macro variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524652#M142688</link>
      <description>&lt;P&gt;Expand the&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.county&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;with a do/end block, and have setting the fname and a put for the header line in it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.county
then do;
  fname = .......;
  put "county,var1,var2,.....";
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The change of the value in fname will cause the current file to be closed and a new one to be opened.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jan 2019 18:55:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524652#M142688</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-04T18:55:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle spaces in a macro variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524672#M142700</link>
      <description>That worked perfectly. Thank you!</description>
      <pubDate>Fri, 04 Jan 2019 19:33:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/524672#M142700</guid>
      <dc:creator>lstockman</dc:creator>
      <dc:date>2019-01-04T19:33:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle spaces in a macro variable list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/771383#M244778</link>
      <description>i used this. very smart solution, thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Thu, 30 Sep 2021 14:18:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-handle-spaces-in-a-macro-variable-list/m-p/771383#M244778</guid>
      <dc:creator>Toni2</dc:creator>
      <dc:date>2021-09-30T14:18:11Z</dc:date>
    </item>
  </channel>
</rss>

