<?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: Creating a string using macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-string-using-macro/m-p/940665#M369193</link>
    <description>&lt;P&gt;Seems simple enough, just use a %DO loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's try it by making a macro that takes those two inputs and emits the values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro model(catvariable,reference);
%local i ;
%do i=1 %to %sysfunc(countw(&amp;amp;catvariable,%str( )));
  %scan(&amp;amp;catvariable,&amp;amp;i,%str( )) (ref="%scan(&amp;amp;reference,&amp;amp;i,%str( ))")
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And let's try it with your example values:&lt;/P&gt;
&lt;PRE&gt;8    %let string=%model(Catvariable = Sex Race RUCC Treatment,Reference = F W U P);
9    %put &amp;amp;=string;
STRING=Sex (ref="F")   Race (ref="W")   RUCC (ref="U")   Treatment (ref="P")&lt;/PRE&gt;
&lt;P&gt;If you want to put the %DO loop in the middle of larger macro then just use a %LET statement in the middle of the %DO loop instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=1 %to %sysfunc(countw(&amp;amp;catvariable,%str( )));
  %let string=&amp;amp;string %scan(&amp;amp;catvariable,&amp;amp;i,%str( )) (ref="%scan(&amp;amp;reference,&amp;amp;i,%str( ))");
%end;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 23 Aug 2024 20:41:37 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-08-23T20:41:37Z</dc:date>
    <item>
      <title>Creating a string using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-string-using-macro/m-p/940657#M369192</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to write a short macro for the regression model, where I need to create a string for the class variable for proc logistics. I have the following information:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let Catvariable = Sex Race RUCC Treatment;&lt;/P&gt;
&lt;P&gt;%let Reference = F W U P;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create a string from the above information, which can be used in the class row of the proc logistics as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sex (ref = 'F') Race (ref= 'W') RUCC (ref='U') Treatment ('P');&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would greatly appreciate any thoughts.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;sandyzman1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2024 19:59:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-string-using-macro/m-p/940657#M369192</guid>
      <dc:creator>sandyzman1</dc:creator>
      <dc:date>2024-08-23T19:59:21Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a string using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-string-using-macro/m-p/940665#M369193</link>
      <description>&lt;P&gt;Seems simple enough, just use a %DO loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's try it by making a macro that takes those two inputs and emits the values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro model(catvariable,reference);
%local i ;
%do i=1 %to %sysfunc(countw(&amp;amp;catvariable,%str( )));
  %scan(&amp;amp;catvariable,&amp;amp;i,%str( )) (ref="%scan(&amp;amp;reference,&amp;amp;i,%str( ))")
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And let's try it with your example values:&lt;/P&gt;
&lt;PRE&gt;8    %let string=%model(Catvariable = Sex Race RUCC Treatment,Reference = F W U P);
9    %put &amp;amp;=string;
STRING=Sex (ref="F")   Race (ref="W")   RUCC (ref="U")   Treatment (ref="P")&lt;/PRE&gt;
&lt;P&gt;If you want to put the %DO loop in the middle of larger macro then just use a %LET statement in the middle of the %DO loop instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=1 %to %sysfunc(countw(&amp;amp;catvariable,%str( )));
  %let string=&amp;amp;string %scan(&amp;amp;catvariable,&amp;amp;i,%str( )) (ref="%scan(&amp;amp;reference,&amp;amp;i,%str( ))");
%end;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Aug 2024 20:41:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-string-using-macro/m-p/940665#M369193</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-23T20:41:37Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a string using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-string-using-macro/m-p/940666#M369194</link>
      <description>&lt;P&gt;Parallel lists are hard to manage.&amp;nbsp; I suggest using a lookup object.&amp;nbsp; In this case an INFORMAT.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro 
   main
      (
         Catvariable = Sex Race RUCC Treatment,
         Reference = RACE=W RUCC=U TREATMENT=P SEX=F ABC=2 XYZ=1
      );

   proc format;
      invalue $ref(upcase just) &amp;amp;reference;
      run;
   %local i w;
   %do i = 1 %to %sysfunc(countw(%superq(catvariable)));
      %let w = %scan(%superq(catvariable),&amp;amp;i);
      %put NOTE: CLASS &amp;amp;w(ref=%sysfunc(inputc(&amp;amp;w,$ref),$quote.));
      %end;
   %mend main;

%main;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;NOTE: CLASS Sex(ref="F"     )
NOTE: CLASS Race(ref="W"     )
NOTE: CLASS RUCC(ref="U"     )
NOTE: CLASS Treatment(ref="P"     )&lt;/PRE&gt;
&lt;P&gt;I would probably define the INFORMAT outside the MACRO but you get the idea.&amp;nbsp; &amp;nbsp;It would be helpful to know the big picture.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2024 20:49:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-string-using-macro/m-p/940666#M369194</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2024-08-23T20:49:47Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a string using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-string-using-macro/m-p/940779#M369236</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much for the macro. It worked perfectly. However, as you suggested, when I&amp;nbsp;&lt;SPAN&gt;use the %DO loop (below) in the middle of a larger macro using a %LET statement. &lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;%do i=1 %to %sysfunc(countw(&amp;amp;catvariable,%str( )));
  %let string=&amp;amp;string %scan(&amp;amp;catvariable,&amp;amp;i,%str( )) (ref="%scan(&amp;amp;reference,&amp;amp;i,%str( ))");
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;It gave me the following error.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;WARNING: Apparent symbolic reference STRING not resolved.&lt;/P&gt;
&lt;P class="p1"&gt;ERROR: The text expression &amp;amp;STRING. SEX (REF="F") contains a recursive reference to the macro variable&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;STRING.&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;The macro variable will be assigned the null value.&lt;/P&gt;
&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Do you have any suggestions? Thanks.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Aug 2024 18:05:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-string-using-macro/m-p/940779#M369236</guid>
      <dc:creator>sandyzman1</dc:creator>
      <dc:date>2024-08-25T18:05:23Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a string using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-string-using-macro/m-p/940781#M369238</link>
      <description>&lt;P&gt;Add&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%local string;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to your macro before the %DO loop.&lt;/P&gt;
&lt;P&gt;It defines the macro variable, and avoids unpleasant side-effects.&lt;/P&gt;</description>
      <pubDate>Sun, 25 Aug 2024 19:30:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-string-using-macro/m-p/940781#M369238</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-08-25T19:30:06Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a string using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-string-using-macro/m-p/940790#M369239</link>
      <description>&lt;P&gt;Make sure STRING is defined BEFORE trying to reference its value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So either set it to an empty string before the %DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let string=;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or define it as a LOCAL macro variable (which will also start as empty.)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%local string;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you might want to do both, depending on whether you want to reuse STRING multiple times in the macro code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Aug 2024 00:08:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-string-using-macro/m-p/940790#M369239</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-26T00:08:44Z</dc:date>
    </item>
  </channel>
</rss>

