<?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: Parsing a macro variable into each word and generate another macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598012#M172393</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279507"&gt;@braam&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;I'd follow &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;'s sage advice and create your required macro variable the way you need from the outset. But if you have your already generated 1 through 8 and want to transform it in the string you've indicated, this will work, however unseemly it may look:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let h = 1 2 3 4 5 6 7 8 ;                                                                                                              
                                                                                                                                        
%let w = contrast %sysfunc (tranwrd (%sysfunc (tranwrd (%str( )&amp;amp;h, %str( ), %str( )id)), %str( ), %str( 1, ))) 1 ;                      
                                                                                                                                        
%put &amp;amp;w ;     
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
    <pubDate>Mon, 21 Oct 2019 03:38:30 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-10-21T03:38:30Z</dc:date>
    <item>
      <title>Parsing a macro variable into each word and generate another macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598002#M172389</link>
      <description>&lt;P&gt;I have a macro variable, ID_LIST, that has a value of "1 2 3 4 6 8". This macro is generated from a dataset (using PROC SQL SELECT). I would like to parse them into each number, and generate another macro variable that is "contrast id1 1, id2 1, id3 1, id4 1, id6 1, id8 1" so that I can use this to perform wald test when I use PROC SURVEYREG. Thanks in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I have: a macro variable having "1 2 3 4 6 8"&lt;/P&gt;
&lt;P&gt;What I would like to have: a macro variable having "contrast id1 1, id2 1, id3 1, id4 1, id6 1, id8 1"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Oct 2019 21:57:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598002#M172389</guid>
      <dc:creator>braam</dc:creator>
      <dc:date>2019-10-20T21:57:12Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a macro variable into each word and generate another macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598005#M172391</link>
      <description>&lt;P&gt;use validvarname = any;&lt;/P&gt;
&lt;P&gt;that option control what you want&lt;/P&gt;
&lt;P&gt;&lt;FONT style="background-color: #ffffff;"&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000279245.htm" target="_blank" rel="noopener"&gt;http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000279245.htm&lt;/A&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Oct 2019 23:21:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598005#M172391</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2019-10-20T23:21:51Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a macro variable into each word and generate another macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598007#M172392</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279507"&gt;@braam&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;….&lt;BR /&gt;This macro is generated from a dataset (using PROC SQL SELECT).&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;What I have: a macro variable having "1 2 3 4 6 8"&lt;/P&gt;
&lt;P&gt;What I would like to have: a macro variable having "contrast id1 1, id2 1, id3 1, id4 1, id6 1, id8 1"&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can use a minor variation of your SELECT clause to generate the comma-separated contrast specification, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select distinct cats('ID',age)||' 1' into :contrast_list separated by ','
  from sashelp.class;
quit;
%put &amp;amp;=contrast_list;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then, later in your analysis code, you can have the statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;contrast &amp;amp;contrast_list ; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Oct 2019 23:38:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598007#M172392</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-10-20T23:38:18Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a macro variable into each word and generate another macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598012#M172393</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279507"&gt;@braam&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;I'd follow &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;'s sage advice and create your required macro variable the way you need from the outset. But if you have your already generated 1 through 8 and want to transform it in the string you've indicated, this will work, however unseemly it may look:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let h = 1 2 3 4 5 6 7 8 ;                                                                                                              
                                                                                                                                        
%let w = contrast %sysfunc (tranwrd (%sysfunc (tranwrd (%str( )&amp;amp;h, %str( ), %str( )id)), %str( ), %str( 1, ))) 1 ;                      
                                                                                                                                        
%put &amp;amp;w ;     
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 03:38:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598012#M172393</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-21T03:38:30Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a macro variable into each word and generate another macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598018#M172394</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let id_list=1 2 3 4 6 8;
%macro code;
id&amp;amp;word 1^
%mend;
%put contrast %seplist(%loop(&amp;amp;id_list),indlm=^);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would put "contrast" in at the proper location in SURVEYREG, rather than in your macro variable.&amp;nbsp; I would also "inject" the commas at the proper location using %seplist, rather than in your macro variable.&amp;nbsp; I never put "syntax" (esp. commas, but also quotes, etc.) in my macro variable metadata if I can help it.&amp;nbsp; Commas can be especially problematic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/loop.sas" target="_blank" rel="noopener"&gt;https://github.com/scottbass/SAS/blob/master/Macro/loop.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/seplist.sas" target="_blank" rel="noopener"&gt;https://github.com/scottbass/SAS/blob/master/Macro/seplist.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/parmv.sas" target="_blank" rel="noopener"&gt;https://github.com/scottbass/SAS/blob/master/Macro/parmv.sas&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 04:46:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598018#M172394</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-10-21T04:46:02Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a macro variable into each word and generate another macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598019#M172395</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15043"&gt;@ScottBass&lt;/a&gt;:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;gt;&lt;SPAN&gt;I would put "contrast" in at the proper location in SURVEYREG, rather than in your macro variable.&amp;lt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Agreed. But in the real world, I'd do it as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;has suggested. There's no need for any of these macro transmutations when the needed list can be composed as required from the outset.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Kind regards&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Paul D.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 04:46:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598019#M172395</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-21T04:46:31Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a macro variable into each word and generate another macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598020#M172396</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15043"&gt;@ScottBass&lt;/a&gt;:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;gt;&lt;SPAN&gt;I would put "contrast" in at the proper location in SURVEYREG, rather than in your macro variable.&amp;lt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Agreed. But in the real world, I'd do it as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;has suggested. There's no need for any of these macro transmutations when the needed list can be composed as required from the outset.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Kind regards&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Paul D.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also agree(ish).&amp;nbsp; I edited my post to indicate why.&amp;nbsp; It's my own coding style but IMO has served me well.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In SAS, as in Perl, TIMTOWTDI.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 04:55:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598020#M172396</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-10-21T04:55:58Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a macro variable into each word and generate another macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598072#M172414</link>
      <description>&lt;P&gt;Thanks! Can I do something like this to keep two lists, one for the original list and the other for the list for contrast statement later? The below code doesn't work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql noprint;
	select distinct age into :age_list separated by ',',
	select distinct cats('ID',age)||' 1' into :contrast_list separated by ','
	from sashelp.class;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Oct 2019 10:38:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598072#M172414</guid>
      <dc:creator>braam</dc:creator>
      <dc:date>2019-10-21T10:38:19Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a macro variable into each word and generate another macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598099#M172421</link>
      <description>&lt;P&gt;Yes, you can generate two lists using one SELECT statement.&amp;nbsp; You list the columns for the select statement, and list the macro variables on the INTO clause, e.g. :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1    proc sql noprint;
2      select distinct
3            age
4          , cat('ID',age,' 1')
5       into :age_list separated by ','
6          , :contrast_list separated by ','
7      from sashelp.class;
8    quit;

9
10
11   %put &amp;gt;&amp;gt;&amp;amp;age_list&amp;lt;&amp;lt; &amp;gt;&amp;gt;&amp;amp;contrast_list&amp;lt;&amp;lt; ;
&amp;gt;&amp;gt;11,12,13,14,15,16&amp;lt;&amp;lt; &amp;gt;&amp;gt;ID11 1,ID12 1,ID13 1,ID14 1,ID15 1,ID16 1&amp;lt;&amp;lt;
&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Oct 2019 12:29:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-macro-variable-into-each-word-and-generate-another/m-p/598099#M172421</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2019-10-21T12:29:21Z</dc:date>
    </item>
  </channel>
</rss>

